asp.net – 哪个会员提供程序实现存储在web.config中的用户?
发布时间:2020-05-27 19:57:57 所属栏目: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
|
有一个代码盲的时刻. 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-2 – 使用ASP.NET MVC 2时更好地保留过滤选项的
- .net – 通过互联网向YH收取的最简单的方法是什么?
- asp.net-mvc-5 – 确认邮件中的aspnet身份无效令牌
- 限制并发或不限制并发? (在单个ASP.NET请求中)
- asp.net-mvc – 如何为其模型调用“null”的局部视图?
- Asp.Net 音频文件上传和播放代码
- asp.net-mvc – 如何将ms显示从LHS移动到RHS
- 在asp.net中使用eval(“”)
- asp-classic – 如何检查VBScript中是否存在POST提交字段?
- asp.net-mvc – 在ASP.Net MVC中使用System.Guid作为主键?
推荐文章
站长推荐
- asp.net – Javascript日期本地化
- ELMAH在ASP.NET vNext?
- asp.net-mvc – ‘Personal’是一种类型,在给定的
- asp.net-core – asp.net核心依赖注入问题 – Ad
- asp.net-mvc – 将索引设置为控制器的默认路由
- ASP.NET MVC ASP.NET WebForms,为什么?
- asp.net-mvc – 在MVC4中为ViewModel设置默认值的
- ASP.NET MVC3角色和权限管理 – 具有运行时权限分
- asp.net-mvc-4 – 如何在ASP.Net MVC 4中定义特定
- asp.net – “’Microsoft.Jet.OLEDB.4.0’提供程
热点阅读
