asp.net – 哪个会员提供程序实现存储在web.config中的用户?
发布时间:2020-12-15 18:59:31 所属栏目:asp.Net 来源:网络整理
导读:有一个代码盲的时刻. ASP.NET 4.0. Web.config文件: ?xml version="1.0"?configuration system.web authentication mode="Forms" forms name="DataViewer" loginUrl="login.aspx" credentials passwordFormat="Clear" user name="devuser" password="test"
有一个代码盲的时刻.
ASP.NET 4.0. Web.config文件: <?xml version="1.0"?> <configuration> <system.web> <authentication mode="Forms"> <forms name="DataViewer" loginUrl="login.aspx"> <credentials passwordFormat="Clear"> <user name="devuser" password="test" /> </credentials> </forms> </authentication> <authorization> <deny users="?" /> </authorization> </system.web> 和登录控件: <asp:Login ID="login" runat="server" /> 如果输入用户名和密码,然后单击登录,它将挂起. 如果我打破,我可以在调用堆栈中看到login.AuthenticateUsingMembershipProvider()在调用SqlMembershipProvider.ValidateUser()的中间.根本没有定义或涉及到这个项目的数据库,我没有指定使用SqlMembershipProvider. 所以我的问题是,我应该使用什么成员资格提供者让ASP.NET使用< credentials>中的用户名和密码. web.config的元素? 解决方法
我很惊讶,考虑到框架设计师如何处理定义“凭证/>”的麻烦.元素,他们没有实现任何代码来消费它.
我发现这个here的一种工作实现,我已经修复并包括在下面. MembershipProvider的所有其他成员抛出NotImplementedException. using System.Configuration; using System.Web.Configuration; using System.Web.Security; public class WebConfigMembershipProvider : MembershipProvider { private FormsAuthenticationUserCollection _users = null; private FormsAuthPasswordFormat _passwordFormat; public override void Initialize(string name,System.Collections.Specialized.NameValueCollection config) { base.Initialize(name,config); _passwordFormat = getPasswordFormat(); } public override bool ValidateUser(string username,string password) { var user = getUsers()[username]; if (user == null) return false; if (_passwordFormat == FormsAuthPasswordFormat.Clear) { if (user.Password == password) { return true; } } else { if (user.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(password,_passwordFormat.ToString())) { return true; } } return false; } protected FormsAuthenticationUserCollection getUsers() { if (_users == null) { AuthenticationSection section = getAuthenticationSection(); FormsAuthenticationCredentials creds = section.Forms.Credentials; _users = section.Forms.Credentials.Users; } return _users; } protected AuthenticationSection getAuthenticationSection() { Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); return (AuthenticationSection)config.GetSection("system.web/authentication"); } protected FormsAuthPasswordFormat getPasswordFormat() { return getAuthenticationSection().Forms.Credentials.PasswordFormat; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – .net Razor替代PHP echo
- .net – SqlConnection的Dispose方法是否会干扰连接池?
- asp.net – 文件 – >在MonoDevelop中打开网站?
- asp.net-mvc – knockout下拉列表将选择传递给MVC5中的模型
- asp.net – HTTPModule BeginRequest应该是Response.Redire
- 如何在ASP.net Identity 2.0上启用AccessFailedCount和Lock
- asp.net-mvc-3 – Hz.Raw()的Razor替代品
- asp.net – 比HttpHandlers快的东西?
- asp.net – 将persistentCookiesOnPassiveRedirects设置为t
- ASP.NET-MVC 2 RC最佳功能
推荐文章
站长推荐
- asp.net – 如何在javascript中检测/跟踪回发?
- asp.net – 如何在客户端上为CustomValidator控件
- asp.net-mvc – MVC表单LoginUrl不正确
- asp.net – jQuery blackberry ajax问题
- ASP.NET CheckBoxList DataBinding问题
- asp.net-mvc – 我可以在_Layout.cshtml中有条件
- asp.net – 以编程方式在ASPX页面中设置’visibl
- asp.net – 有没有办法将动态文本应用于webform上
- asp.net – DataList中的分页无法正常工作
- 在ASP.NET MVC中为JavaScript生成动作URL
热点阅读