在 IIS 7.0 中使用自定义 MembershipProvider 和 Roleprovider
我正在编写一个 ASP.NET MVC 3 网站。这是我的自定义 MembershipProvider(仅实现了 ValidateUser):
public class RFMMembershipProvider : MembershipProvider
{
IUserService userService = new UserService();
public override bool ValidateUser(string username, string password)
{
return password.GetHashCode().ToString() == userService.GetUser(username).Pass;
}
...
}
和我的 Roleprovider(仅实现了 GetRolesForUser)
public class RFMRoleProvider : RoleProvider
{
IUserService userService = new UserService();
public override string[] GetRolesForUser(string username)
{
return new string[] { userService.GetRolesForUser(username).Name };
}
...
}
我的 web.config 部分
...
<system.web>
<roleManager enabled="true" defaultProvider="RFMRoleProvider">
<providers>
<clear/>
<add name="RFMRoleProvider" type="RFMSite.WebUI.RFMRoleProvider, RFMSite"/>
</providers>
</roleManager>
<membership defaultProvider="RFMMembershipProvider"
>
<providers>
<clear/>
<add name="RFMMembershipProvider"
type="RFMSite.WebUI.RFMMembershipProvider, RFMSite"
/>
</providers>
</membership>
<authentication mode="Forms" >
<forms loginUrl="~/Account/LogOn" timeout="2880">
</forms>
</authentication>
登录操作:
...
if (Membership.ValidateUser(username, password))
{
FormsAuthentication.SetAuthCookie(username, true);
return RedirectToAction("Files", "Admin");
}
...
return View();
所以问题是为什么当我在 IIS 7.0 上发布网站时 Membership.ValidateUser(用户名,密码) )
总是返回 false?它在本地 asp.net 开发服务器上正常工作。 与MSSQL Server的连接是否正常(我可以获取任何数据并在网站部署时显示它)?没有发生异常,只是总是返回 false...
I'm writing a ASP.NET MVC 3 web site. This is my custom MembershipProvider (only ValidateUser realized):
public class RFMMembershipProvider : MembershipProvider
{
IUserService userService = new UserService();
public override bool ValidateUser(string username, string password)
{
return password.GetHashCode().ToString() == userService.GetUser(username).Pass;
}
...
}
and my Roleprovider (realized only GetRolesForUser)
public class RFMRoleProvider : RoleProvider
{
IUserService userService = new UserService();
public override string[] GetRolesForUser(string username)
{
return new string[] { userService.GetRolesForUser(username).Name };
}
...
}
my web.config part
...
<system.web>
<roleManager enabled="true" defaultProvider="RFMRoleProvider">
<providers>
<clear/>
<add name="RFMRoleProvider" type="RFMSite.WebUI.RFMRoleProvider, RFMSite"/>
</providers>
</roleManager>
<membership defaultProvider="RFMMembershipProvider"
>
<providers>
<clear/>
<add name="RFMMembershipProvider"
type="RFMSite.WebUI.RFMMembershipProvider, RFMSite"
/>
</providers>
</membership>
<authentication mode="Forms" >
<forms loginUrl="~/Account/LogOn" timeout="2880">
</forms>
</authentication>
on LogOn action:
...
if (Membership.ValidateUser(username, password))
{
FormsAuthentication.SetAuthCookie(username, true);
return RedirectToAction("Files", "Admin");
}
...
return View();
So the question is Why when I publish site on IIS 7.0 Membership.ValidateUser(username, password)
always returns false? It works NORMAL on local asp.net development server.
The connection with MSSQL Server is OK (I can get any data and display it when website deployed)? No exception happens, just always returns false...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定在本地和生产中调用完全相同的代码吗?我怀疑你的代码除非意外,否则永远不会工作。
具体来说,我怀疑
String.GetHashCode()
是否会返回与数据库中的密码相匹配的内容,除非您的用户习惯于使用长随机数作为密码。GetHashCode
用于构建哈希表,而不是用于保护密码。我认为您将它与 HashPasswordForStoringInConfigFile 或类似的东西混淆了。为了清楚起见进行编辑:
我不确定
GetHashCode
是您的问题,但我没有看到其他任何明显错误。测试起来很容易:将哈希码记录到日志文件中,因为它们无论如何都是临时的(正如您在评论中指出的那样)。是的,GetHashCode 可以在部署时轻松更改;例如,如果您正在运行不同的体系结构,或者针对较新版本的框架,则从 GetHashCode 返回的确切值肯定会有所不同。
Are you sure you're calling the exact same code locally and in production? I'm suspicious that your code will ever work except by accident.
Specifically, I doubt that
String.GetHashCode()
will ever return something that matches a password from your database, unless your users are in the habit of using long random numbers for their passwords.GetHashCode
is for building hash tables, not for securing passwords. I think you are confusing it withHashPasswordForStoringInConfigFile
, or something similar.EDIT for clarity:
I don't know for sure that
GetHashCode
is your problem but I don't see anything else obviously wrong. It would be easy enough to test: log your hash codes to a log file, since they're temporary anyway (as you noted in the comment).And yes, GetHashCode could easily change when you deployed; if you are running a different architecture, for example, or against a newer version of the Framework, the exact value that gets returned from GetHashCode can definitely be different.
不要将
GetHashCode
用于此目的!使用始终相同的 MD5 或 SHA1 安全哈希值。GetHashCode
不同版本之间可以返回不同的值.NET框架。在 32 位和 64 位系统上使用时,它还会返回不同的值。Don't use
GetHashCode
for this purpose! Use MD5 or SHA1 security hashes which are always the same.GetHashCode
can return different values between different versions of .NET framework. It also returns different values when used on 32-bit and 64-bit system.