简体   繁体   中英

unable to redirect to login page after using forms authentication

Before applying forms authentication on login page I am able to redirect to login and registration page from website's homepage but after applying form authentication on login page, whenever I tried to redirect to login page it shows me error(below image): 在此处输入图片说明

I can't find out where I am making mistake. I have posted .cs code of login page and web.config file . have a look over it. show me where I am making mistake and what is the solution.

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>

Remember that using FormsAuthentication.RedirectFromLoginPage requires Username and persistence , so rd["Role"].ToString() won't work.

Follow this example as it will give you the quick solution. FormsAuthentication.RedirectFromLoginPage to a custom page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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