Web.配置文件中的授权在正确凭据后不起作用
本文关键字:不起作用 配置文件 授权 Web | 更新日期: 2023-09-27 18:32:29
每当用户未登录时,如果他将页面名称直接放在URL中,他应该被定向到网站的登录页面。
我在我的 web.config 文件中进行了身份验证,如下所示:-
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" timeout="2880" defaultUrl="~/" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
但是当我使用正确的凭据登录时,它仍然保留在登录页面中。
另请参阅我的登录按钮代码:-
protected void btnSubmit_Click(object sender, EventArgs e)
{
String LoginID = txtUsername.Text.Trim().ToLower();
String LoginPassword = txtPassword.Text.Trim();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("select username,password,usertype from tbl_User where username =@username and password=@password and Active= 1 ", conn);
FormsAuthentication.SetAuthCookie("2", false);
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt != null && dt.Rows.Count > 0)
{
if (dt.Rows[0]["usertype"].ToString() == "0") //SuperAdmin
{
Session["User"] = "0";
Response.Redirect("csrpage.aspx");
}
else if (dt.Rows[0]["usertype"].ToString() == "1") // Admin
{
Session["User"] = "1";
Response.Redirect("Admin.aspx");
}
else if (dt.Rows[0]["usertype"].ToString() == "2") // User
{
Session["User"] = "2";
Response.Redirect("User.aspx");
}
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
请帮忙。
从以下链接获得解决方案:-
身份验证问题对我来说,主要部分是在下面添加的代码:-
<location path="User">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
当您确认用户名和密码正确时,您需要拨打FormsAuthentication.SetAuthCookie("username", false);