繁体   English   中英

使用表单身份验证后无法重定向到登录页面

[英]unable to redirect to login page after using forms authentication

在登录页面上应用表单身份验证之前,我可以从网站主页重定向到登录和注册页面,但是在登录页面上应用表单身份验证后,每当我尝试重定向到登录页面时,它都会显示错误(下图): 在此处输入图片说明

我不知道我哪里出错了。 我已经发布了登录页面和 web.config 文件的 .cs 代码。 看一看。 告诉我我在哪里犯了错误以及解决方案是什么。

登录页面.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;

    }
}

}

网页配置

 <?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>

请记住,使用FormsAuthentication.RedirectFromLoginPage需要Usernamepersistence ,因此rd["Role"].ToString()将不起作用。

请遵循此示例,因为它将为您提供快速解决方案。 FormsAuthentication.RedirectFromLoginPage 到自定义页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM