Authentication through web.config not authenticating in ASP.net 3.5

对着背影说爱祢 提交于 2019-11-29 03:45:30

I'm not sure if this has changed in .NET 3.5, but the <credentials> element has an attribute passwordFormat that defines the format for passwords in the web.config. From the MSDN documentation for .NET 3.5, the default format is SHA1.

If you're using cleartext usernames and passwords in your web.config, you should use:

...
<credentials passwordFormat="Clear">
...

Event though this is an internal application I'd still recommend at least hashing the password instead of leaving it in clear text.

I think the reason is because you did not indicate the passwordFormat. http://msdn.microsoft.com/en-us/library/e01fc50a.aspx

Default is SHA1, hence your clear text in fact not used properly.

You have to specify <credentials passwordFormat="Clear"> when you store password in clear text.

The alternatives are encrypted passwords using MD5 or SHA1.

See http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.hashpasswordforstoringinconfigfile.aspx for a function to encode a password.

You might also consider using some of the available user controls that does a lot for you automatically. Look under the "Login" section in the control toolbox in Visual Studio.

The following page will provide everything you need for this simple case, and the looks of the Login control is fully customizable:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        e.Authenticated = FormsAuthentication.Authenticate(Login1.UserName, Login1.Password);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" onauthenticate="Login1_Authenticate">
        </asp:Login>
    </div>
    </form>
</body>
</html>

Another possible pitfall that is that the user name "Admin" appears to be special and not honored if your testing a credential set in web.config

<credentials>
 <user name="Admin" password="somepassword" />  //Authentication always returns false for me
</credentials>

<credentials>
 <user name="MyName" password="somepassword" />  //Authentication works normally 
</credentials>

The problem doesn't seem to apply in your case, but I just spent an hour figuring that out so I thought I'd record it here.

i find that solution........first you have to get hashvalue by using FormsAuthentication.HashPasswordForStoringInConfigFile("abc","SHA1") in text box by running your program and then provide this value in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!