.NET 4.5 中的窗体身份验证

本文关键字:窗体 身份验证 NET | 更新日期: 2023-09-27 17:57:10

>我通常使用表单身份验证作为登录表单

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
   if(FormsAuthentication.Authenticate(Login1.UserName, Login1.Password))
     {
        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
     }
}

web.config

<authentication mode="Forms">
  <forms defaultUrl="Page.aspx">
     <credentials passwordFormat="Clear">
        <user name="Enter here" password="Enter here" />
     </credentials>
  </forms>
</authentication>
<authorization>
  <deny users="?" />
</authorization>

但是,这不适用于 .NET 4.5。什么是更好的方法?

.NET 4.5 中的窗体身份验证

public void Login_OnClick(object sender, EventArgs args)
{
    if (Membership.ValidateUser(UsernameTextbox.Text, PasswordTextbox.Text))
    {
        FormsAuthentication.RedirectFromLoginPage(UsernameTextbox.Text,     NotPublicCheckBox.Checked);
    }
    else
    {
        Msg.Text = "Login failed. Please check your user name and password and try again.";
    }
}