我正在尝试创建一个用户可以在我的网站上登录的身份验证 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 时,我做错了什么?