在登录页面上应用表单身份验证之前,我可以从网站的主页重定向到登录和注册页面,但在登录页面上应用表单身份验证后,每当我尝试重定向到登录页面时,它都会显示错误(下面图片):
我无法找出我犯错误的地方。我发布了登录页面和web.config文件的.cs代码。仔细看看。告诉我我在哪里犯错误,解决方案是什么。
LoginPage.aspx.cs: -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Security;
public partial class Registration_LoginPage : System.Web.UI.Page
{
Code code = new Code();
SqlConnection con;
SqlCommand cmd;
bool flag = true;
public Registration_LoginPage()
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
cmd = new SqlCommand();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
}
if(User.Identity.Name !=String.Empty)
{
FormsAuthentication.RedirectFromLoginPage(User.Identity.Name, false);
}
}
protected void btnLogIn_Click(object sender, EventArgs e)
{
// String encryptedPassword = code.encrypt(Request.Form["password"]);
try
{
con.Open();
cmd.CommandText = "select * from [Users]";
cmd.Connection = con;
SqlDataReader rd = cmd.ExecuteReader();
if (Request.Form["username"] == "admin" && Request.Form["password"] == "admin")
{
Session["Username"] = Request.Form["username"];
Response.Redirect("/AdminHome/AdminMPage.aspx");
}
else
{
while (rd.Read())
{
if (rd["UserName"].ToString() == Request.Form["username"] && rd["Password"].ToString() == Request.Form["password"])
{
Session["Username"] = rd["UserName"];
flag = false;
break;
}
}
if (flag == true)
lblMsg.Text = "Username and password invalid";
else
{
if (rd["Role"].ToString() == "Student")
// Response.Redirect("Student.aspx");
FormsAuthentication.RedirectFromLoginPage(rd["Role"].ToString(), false);
/* else
Response.Redirect("Teacher.aspx"); */
if (rd["Role"].ToString() == "Teacher")
FormsAuthentication.RedirectFromLoginPage(rd["Role"].ToString(), false);
}
}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
}
}
的web.config
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms loginUrl="/Registration/LoginPage.aspx">
</forms>
</authentication>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<location path="FIRST PAGE">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="Registration">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
<location path="AdminHome">
<system.web>
<authorization>
<allow users="admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="Student">
<system.web>
<authorization>
<allow roles="Student"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="Teacher">
<system.web>
<authorization>
<allow roles="Teacher"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
</configuration>
&#13;
答案 0 :(得分:1)
请注意,使用FormsAuthentication.RedirectFromLoginPage
需要Username
和persistence
,因此rd["Role"].ToString()
无效。
请按照此示例,因为它将为您提供快速解决方案。 FormsAuthentication.RedirectFromLoginPage to a custom page