Asp.net 哪个成员资格提供程序实现存储在web.config中的用户?
有代码盲的时刻 ASP.NET4.0 Web.config:Asp.net 哪个成员资格提供程序实现存储在web.config中的用户?,asp.net,membership-provider,Asp.net,Membership Provider,有代码盲的时刻 ASP.NET4.0 Web.config: <?xml version="1.0"?> <configuration> <system.web> <authentication mode="Forms"> <forms name="DataViewer" loginUrl="login.aspx"> <credentials passwordFormat="Clear"&g
<?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" />
如果我输入用户名和密码,然后单击登录,它将挂起
如果我中断,我可以在调用堆栈中看到,<代码>登录。AudioTestAuthMeNestStasePosivor()/代码>位于调用<代码> SqLMeMistPosivals.ValueAuthor()/<代码>的中间。这个项目中根本没有定义或涉及数据库,我也没有指定应该使用SqlMembershipProvider
因此,我的问题是,我应该使用哪个成员资格提供商让ASP.NET使用
web.config
的
元素中的用户名和密码?尝试使用此方法。我希望有帮助 您需要为此编写自己的提供程序。在中获取示例readonlyxmlmmembershipProvider
,并将其更改为从web.config
读取用户和凭据,应该相对简单,而不是外部XML文件。考虑到框架设计人员如何费劲定义
元素,却没有实现任何代码来使用它,我感到惊讶
我发现了一种工作实现,我已经修复了它,并将其包含在下面。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;
}
}
我不确定你是否试过,但是 负责为您这样做(尽管现在不推荐使用它,因为推荐的行为是使用Membership对象) 从MSDN: Authenticate方法验证存储在应用程序配置文件的凭据部分中的用户凭据。或者,您可以使用ASP.NET成员身份存储用户凭据,并调用ValidateUser验证凭据 您还可以删除成员资格提供程序(因为即使您没有在web.config上声明它们,它们也是从machine.config文件继承的)
可能是因为默认成员资格提供程序是AspNetSqlProvider,它使用SQL Server数据库作为其用户存储。我希望这是一个临时解决方案。在web.config中维护凭据将变得非常困难,因为每次更改都会重新启动web应用程序。但是,它适合当前的场景。我需要为密码永远不会更改的单个用户(曾经)提供简单应用程序的微不足道的保护。具有更大依赖关系的解决方案不合适。我正要使用不推荐的方法FormsAuthentication.Authenticate。谢谢你把课堂安排在这里。
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
</providers>
</membership>