当我在asp.net的登录页面输入用户名和密码时,检查后用户没有被重定向到管理页面。
这是我的代码:
if (Page.IsValid)
{
if (Page.User.Identity.IsAuthenticated)
{
Response.Redirect("~/ManagePage/Default.aspx");
}
else
{
if (CheckLogin(UserName.Text.Trim(), Password.Text.Trim()) == true)
{
FormsAuthentication.RedirectFromLoginPage(UserName.Text.Trim(), false);
if (Roles.GetRolesForUser(UserName.Text.Trim())[0] == "Admin")
{
Session["CounterLogin"] = 0;
Response.Redirect("~/ManagePage/Default.aspx");
}
else if (Roles.GetRolesForUser(UserName.Text)[0] == "User")
{
Session["CounterLogin"] = 0;
Response.Redirect("~/Default.aspx");
}
}
else
{
FailureText.Text = "Please Check UserName And Password";
}
}
}
我在其他可行的项目中使用此代码,但在我的新项目中它不起作用。
答案 0 :(得分:0)
尝试使用FormsAuthentication.SetAuthCookie进行登录:
FormsAuthentication.SetAuthCookie(UserName.Text.Trim(), false);
并选中在web.config中启用FormsAuthentication
<authentication mode="Forms">
<forms loginUrl="~/Home/Login" >
</forms>
</authentication>