1. 配置 web.config

<membership defaultProvider="AspNetReadOnlyXmlMembershipProvider">
<providers>
<clear />
<add name="AspNetReadOnlyXmlMembershipProvider" type="OpenIdWebRingSsoProvider.Code.ReadOnlyXmlMembershipProvider" description="Read-only XML membership provider" xmlFileName="~/App_Data/Users.xml" />
</providers>
</membership>

2.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Security.Permissions;
using System.Web;
using System.Web.Hosting;
using System.Web.Security;
using System.Xml; public class ReadOnlyXmlMembershipProvider : MembershipProvider {
private Dictionary<string, MembershipUser> users;
private string xmlFileName; // MembershipProvider Properties
public override string ApplicationName {
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
} public override bool EnablePasswordRetrieval {
get { return false; }
} public override bool EnablePasswordReset {
get { return false; }
} public override int MaxInvalidPasswordAttempts {
get { throw new NotSupportedException(); }
} public override int MinRequiredNonAlphanumericCharacters {
get { throw new NotSupportedException(); }
} public override int MinRequiredPasswordLength {
get { throw new NotSupportedException(); }
} public override int PasswordAttemptWindow {
get { throw new NotSupportedException(); }
} public override MembershipPasswordFormat PasswordFormat {
get { throw new NotSupportedException(); }
} public override string PasswordStrengthRegularExpression {
get { throw new NotSupportedException(); }
} public override bool RequiresQuestionAndAnswer {
get { throw new NotSupportedException(); }
} public override bool RequiresUniqueEmail {
get { throw new NotSupportedException(); }
} // MembershipProvider Methods
public override void Initialize(string name, NameValueCollection config) {
// Verify that config isn't null
if (config == null) {
throw new ArgumentNullException("config");
} // Assign the provider a default name if it doesn't have one
if (string.IsNullOrEmpty(name)) {
name = "ReadOnlyXmlMembershipProvider";
} // Add a default "description" attribute to config if the
// attribute doesn't exist or is empty
if (string.IsNullOrEmpty(config["description"])) {
config.Remove("description");
config.Add("description", "Read-only XML membership provider");
} // Call the base class's Initialize method
base.Initialize(name, config); // Initialize _XmlFileName and make sure the path
// is app-relative
string path = config["xmlFileName"]; if (string.IsNullOrEmpty(path)) {
path = "~/App_Data/Users.xml";
} if (!VirtualPathUtility.IsAppRelative(path)) {
throw new ArgumentException("xmlFileName must be app-relative");
} string fullyQualifiedPath = VirtualPathUtility.Combine(
VirtualPathUtility.AppendTrailingSlash(HttpRuntime.AppDomainAppVirtualPath),
path); this.xmlFileName = HostingEnvironment.MapPath(fullyQualifiedPath);
config.Remove("xmlFileName"); // Make sure we have permission to read the XML data source and
// throw an exception if we don't
FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, this.xmlFileName);
permission.Demand(); // Throw an exception if unrecognized attributes remain
if (config.Count > ) {
string attr = config.GetKey();
if (!string.IsNullOrEmpty(attr)) {
throw new ProviderException("Unrecognized attribute: " + attr);
}
}
} public override bool ValidateUser(string username, string password) {
// Validate input parameters
if (string.IsNullOrEmpty(username) ||
string.IsNullOrEmpty(password)) {
return false;
} try {
// Make sure the data source has been loaded
this.ReadMembershipDataStore(); // Validate the user name and password
MembershipUser user;
if (this.users.TryGetValue(username, out user)) {
if (user.Comment == password) { // Case-sensitive
// NOTE: A read/write membership provider
// would update the user's LastLoginDate here.
// A fully featured provider would also fire
// an AuditMembershipAuthenticationSuccess
// Web event
return true;
}
} // NOTE: A fully featured membership provider would
// fire an AuditMembershipAuthenticationFailure
// Web event here
return false;
} catch (Exception) {
return false;
}
} public override MembershipUser GetUser(string username, bool userIsOnline) {
// Note: This implementation ignores userIsOnline // Validate input parameters
if (string.IsNullOrEmpty(username)) {
return null;
} // Make sure the data source has been loaded
this.ReadMembershipDataStore(); // Retrieve the user from the data source
MembershipUser user;
if (this.users.TryGetValue(username, out user)) {
return user;
} return null;
} public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) {
// Note: This implementation ignores pageIndex and pageSize,
// and it doesn't sort the MembershipUser objects returned // Make sure the data source has been loaded
this.ReadMembershipDataStore(); MembershipUserCollection users = new MembershipUserCollection(); foreach (KeyValuePair<string, MembershipUser> pair in this.users) {
users.Add(pair.Value);
} totalRecords = users.Count;
return users;
} public override int GetNumberOfUsersOnline() {
throw new NotSupportedException();
} public override bool ChangePassword(string username, string oldPassword, string newPassword) {
throw new NotSupportedException();
} public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) {
throw new NotSupportedException();
} public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) {
throw new NotSupportedException();
} public override bool DeleteUser(string username, bool deleteAllRelatedData) {
throw new NotSupportedException();
} public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) {
throw new NotSupportedException();
} public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) {
throw new NotSupportedException();
} public override string GetPassword(string username, string answer) {
throw new NotSupportedException();
} public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) {
throw new NotSupportedException();
} public override string GetUserNameByEmail(string email) {
throw new NotSupportedException();
} public override string ResetPassword(string username, string answer) {
throw new NotSupportedException();
} public override bool UnlockUser(string userName) {
throw new NotSupportedException();
} public override void UpdateUser(MembershipUser user) {
throw new NotSupportedException();
} // Helper method
private void ReadMembershipDataStore() {
lock (this) {
if (this.users == null) {
this.users = new Dictionary<string, MembershipUser>(, StringComparer.InvariantCultureIgnoreCase);
XmlDocument doc = new XmlDocument();
doc.Load(this.xmlFileName);
XmlNodeList nodes = doc.GetElementsByTagName("User"); foreach (XmlNode node in nodes) {
MembershipUser user = new MembershipUser(
Name, // Provider name
node["UserName"].InnerText, // Username
null, // providerUserKey
null, // Email
string.Empty, // passwordQuestion
node["Password"].InnerText, // Comment
true, // isApproved
false, // isLockedOut
DateTime.Now, // creationDate
DateTime.Now, // lastLoginDate
DateTime.Now, // lastActivityDate
DateTime.Now, // lastPasswordChangedDate
new DateTime(, , )); // lastLockoutDate this.users.Add(user.UserName, user);
}
}
}
}
}

3.

谢谢浏览!

自定义一个叫 ReadOnlyXmlMembershipProvider 的 MembershipProvider,用 XML 作为用户储藏室的更多相关文章

  1. Spring自定义一个拦截器类SomeInterceptor,实现HandlerInterceptor接口及其方法的实例

    利用Spring的拦截器可以在处理器Controller方法执行前和后增加逻辑代码,了解拦截器中preHandle.postHandle和afterCompletion方法执行时机. 自定义一个拦截器 ...

  2. JSTL,自定义一个标签的功能案例

    1.自定义一个带有两个属性的标签<max>,用于计算并输出两个数的最大值: 2.自定义一个带有一个属性的标签<lxn:readFile  src=“”>,用于输出指定文件的内容 ...

  3. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  4. 自定义一个EL函数

    自定义一个EL函数 一般就是一下几个步骤,顺便提供一个工作常用的 案例: 1.编写一个java类,并编写一个静态方法(必需是静态方法),如下所示: public class DateTag { pri ...

  5. 自定义一个可以被序列化的泛型Dictionary<TKey,TValue>集合

    Dictionary是一个键值类型的集合.它有点像数组,但Dictionary的键可以是任何类型,内部使用Hash Table存储键和值.本篇自定义一个类型安全的泛型Dictionary<TKe ...

  6. SpringMVC 自定义一个拦截器

    自定义一个拦截器方法,实现HandlerInterceptor方法 public class FirstInterceptor implements HandlerInterceptor{ /** * ...

  7. jQuery Validate 表单验证插件----自定义一个验证方法

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二.引入依赖包 <script src="../../scripts/j ...

  8. Volley HTTP库系列教程(5)自定义一个Volley请求

    Implementing a Custom Request Previous  Next This lesson teaches you to Write a Custom Request parse ...

  9. 在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性:

    在String()构造器不存在的情况下自定义一个MyString()函数,实现如下内建String()方法和属性: var s = new MyString("hello"); s ...

随机推荐

  1. PHP--------memcache技术

    新事物的产生都不是偶然的 1.为什么会产生memcache? 在大型的电商web页面上,数据量庞大,大量用户需要同时访问海量的数据,为了提高用户的访问效果,如何才能让页面加载最快,更友好的展示到用户面 ...

  2. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John ...

  3. SAE部署django应用

    最近自己动手实现了一个博客系统,使用基于python的web框架django,运行在SAE上.以下是遇到的问题,特总结如下: 1, SAE支持的django等第三方模块的版本如下: http://ww ...

  4. Avizo应用 - Home和Set Home

    Avizo的数据展示区域中两个选项Home和Set Home,如下图: 接下来会通过一套岩心的数据处理,解释一下这两个选项的一个用处. 首先这个数据已经完成了过滤处理,体渲染效果如下: 然后进行数据分 ...

  5. 关于H.264 x264 h264 AVC1

    1. H.264是MPEG4的第十部分,是一个标准.对头,国际上两个视频专家组(VCEG和MPEG)合作提出的标准,两个专家组各有各的叫法,所以既叫H.264,也叫AVC. 2.x264是一个编码器, ...

  6. find the peak value

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  7. ldr和adr在使用标号表达式作为操作数的区别

    ARM汇编有ldr指令以及ldr.adr伪指令,他门都可以将标号表达式作为操作数,下面通过分析一段代码以及对应的反汇编结果来说明它们的区别. ldr     r0, _start adr     r0 ...

  8. Scala 深入浅出实战经典 第60讲:Scala中隐式参数实战详解以及在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  9. 使用sqlserver的游标功能来导数据的常见写法

    一定要自己试过才知道么? 你也没试过吃屎,你怎么知道屎不能吃,难道你试过啊...(没有愤怒的意思) ),),) declare cursor_data CURSOR FOR SELECT [UserN ...

  10. asp.net web 后台判断提示框,点击'是'执行代码A(),点击'否'执行代码B()

    html code <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server&q ...