2

我正在尝试创建一个用户可以在我的网站上登录的身份验证 cookie。

我有一个这样的登录表单:

<asp:TextBox ID="txtUsername" runat="server" MaxLength="10" Text="Sam001" ></asp:TextBox>
<asp:TextBox ID="txtPassword" runat="server" MaxLength="10" Text="Pass01" ></asp:TextBox>

<asp:Label ID="status" runat="server" ></asp:Label>

<asp:Button CssClass="button" ID="Submit" runat="server" Text="Logga in" OnClick="Login_Click" />

然后该按钮在后面的代码中执行此操作:

protected void Login_Click(object sender, EventArgs e)
{
    DbReader listData = new DbReader();
    Employee tempEmp = null;

    if ((tempEmp = listData.GetUser(txtUsername.Text, txtPassword.Text)) != null) // check if username and pw was correct
    {
        FormsAuthentication.SetAuthCookie(tempEmp.EID, false); // create auth cookie

        Debug.WriteLine("auth cookie set for: " + tempEmp.EID);

        if (FormsAuthentication.Authenticate(tempEmp.EID, txtPassword.Text)) // check if name and pass is valid
        {
            Debug.WriteLine("auth validation ok");

            FormsAuthentication.RedirectFromLoginPage(tempEmp.EID, false); // redirect


            status.Text = User.Identity.Name; // set status to the Name property of the auth cookie
        }
        else
        {
            status.Text = "failed to Authenticate";
        }
    }
    else
    {
        status.Text = "failed to get user";
    }
}

在 Web.config 中它看起来像这样:

<authentication mode="Forms">
  <forms name="LoggedInUser" loginUrl="~/Login.aspx" protection="All" timeout="10" path="/" />
</authentication>

为什么我总是“无法验证”?当我想创建登录用户需要访问某些页面的身份验证 cookie 时,我做错了什么?

4

2 回答 2

3

Authenticate 方法适用于存储在 web.config 中的用户和密码列表。

所以要使用它,你的 web.config 需要看起来像:

<authentication mode="Forms">
  <forms name="LoggedInUser" loginUrl="~/Login.aspx" protection="All" timeout="10" path="/" />
    <credentials passwordFormat="SHA1">
      <user name="user1" password="27CE4CA7FBF00685AF2F617E3F5BBCAFF7B7403C" />
      <user name="user2" password="D108F80936F78DFDD333141EBC985B0233A30C7A" />
      <user name="user3" password="7BDB09781A3F23885CD43177C0508B375CB1B7E9"/>
    </credentials>
  </forms>
</authentication>

此示例是从描述身份验证方法的Microsoft 页面获得的。

此外,重要的是要注意 Microsoft 已宣布此方法已过时,并建议改用其中一个成员资格提供程序。

于 2013-12-22T00:59:47.703 回答
0

如果我错了,请原谅我,但是

这可能与卷有关。

我的项目也有类似的问题,但我发现了;对于每个用户,您要在用户表中分配一个卷。

在 web.config 中,您需要添加类似于以下内容的内容。

        <authorization>
            <!-- Order and case are important below -->
            <allow roles="User"/>
            <deny users="*"/>
        </authorization>

我使用本指南来帮助我开始...

基于滚动的身份验证

于 2013-12-22T00:47:14.860 回答