通过 web.config 进行身份验证未在 ASP.net 3.5 中进行身份验证

发布于 2024-07-27 23:34:23 字数 1010 浏览 2 评论 0原文

这是应该非常简单的事情之一,我只是不明白为什么它不起作用。

我正在尝试为 ASP.net 3.5 应用程序设置一些非常快速的身份验证,但将用户名和密码存储在 web.config 文件中(我知道它不是很安全,但它是一个内部应用程序,我不断被要求添加和删除登录信息,因此这是最快的方法)。

因此,相关的配置部分如下所示:

<authentication mode="Forms">
   <forms loginUrl="~/login.aspx">
    <credentials>
     <user name="user" password="password" />
     <user name="user2" password="password2" />
    </credentials>
   </forms>
  </authentication>

  <authorization>
    <deny users="?"/>
  </authorization>

并且,在登录页面中,代码如下所示:

string username = tbUsername.Text;
string password = tbPassword.Text;

if (FormsAuthentication.Authenticate(username, password))
    FormsAuthentication.RedirectFromLoginPage(username, false);

但是,FormsAuthentication.Authenticate(用户名,密码) 始终返回 false。 我不明白为什么。

我什至尝试使用 Membership.ValidateUser 但这只是将本地数据库添加到 App_Data 文件夹中。

是否有一些非常基本的东西我在这里忘记了,或者这在 .net 3.5 中根本不起作用?

This is one of this things that should be extremely simple and I just can't work out why it's not working.

I'm trying to set up some very quick authentication for an ASP.net 3.5 app but storing the usernames and passwords in the web.config file (I know it's not very secure but it's an internal app that I keep getting asked to add and remove logins for so this is the quickest way to do it).

So, the relevant config section looks like this:

<authentication mode="Forms">
   <forms loginUrl="~/login.aspx">
    <credentials>
     <user name="user" password="password" />
     <user name="user2" password="password2" />
    </credentials>
   </forms>
  </authentication>

  <authorization>
    <deny users="?"/>
  </authorization>

And, in the login page, the code look like this:

string username = tbUsername.Text;
string password = tbPassword.Text;

if (FormsAuthentication.Authenticate(username, password))
    FormsAuthentication.RedirectFromLoginPage(username, false);

But, FormsAuthentication.Authenticate(username, password) always returns false. And I can't figure out why.

I even tried using Membership.ValidateUser but that just adds in a local database to the App_Data folder.

Is there something really basic I'm forgetting here or does this not work at all in .net 3.5?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

掩耳倾听 2024-08-03 23:34:23

我不确定这在 .NET 3.5 中是否已更改,但 元素具有一个属性 passwordFormat,它定义 中密码的格式web.config。 从 .NET 3.5 的 MSDN 文档 中,默认格式为 SHA1。

如果您在 web.config 中使用明文用户名和密码,则应使用:

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

事件,尽管这是一个内部应用程序,但我仍然建议至少对密码进行哈希处理,而不是将其保留为明文。

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.

↙厌世 2024-08-03 23:34:23

我认为原因是因为你没有指明passwordFormat。
http://msdn.microsoft.com/en-us/library/e01fc50a。 aspx

默认是 SHA1,因此您的明文实际上没有正确使用。

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.

离旧人 2024-08-03 23:34:23

当您以明文形式存储密码时,必须指定

替代方案是使用 MD5 或 SHA1 加密密码。

请参阅 http://msdn.microsoft.com /en-us/library/system.web.security.formsauthentication.hashpasswordforstoringinconfigfile.aspx 用于对密码进行编码的函数。

您还可以考虑使用一些可用的用户控件,它们可以自动为您做很多事情。 查看 Visual Studio 控件工具箱中的“登录”部分。

以下页面将提供这个简单案例所需的一切,并且登录控件的外观是完全可定制的:

<%@ 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>

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>
往日 2024-08-03 23:34:23

另一个可能的陷阱是,如果您测试 web.config 中设置的凭据,则用户名“Admin”似乎很特殊并且不受尊重。

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

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

问题似乎不适用于您的情况,但我只是花了一个小时才弄清楚这一点所以我想我应该把它记录在这里。

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.

旧城空念 2024-08-03 23:34:23

我找到了这个解决方案......首先,您必须通过运行程序在文本框中使用 FormsAuthentication.HashPasswordForStoringInConfigFile("abc","SHA1") 来获取哈希值,然后在

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文