简体   繁体   中英

how to redirect user from login page to next page base on their Role in Asp.net C#

I am using built-in asp.net Role and membership provider in my website.

when user login to my system they are redirected to hompage.

how should i code it. that when he click on login button page check its role and then decide where to redirect. Suppose user login with name John and "john" is "Admin" then application Redirect him to AdminPanel.aspx and if User john is normal "RegUser" Role then redirect him into Home.aspx.

Thanks In advance........

On your login page, if the user was successfully logged in, you could store their role in the Session and in your Home.aspx just have a bit of logic which determines whether they need to be re-directed away from this page or not eg

login.aspx

void LoginBtn_Click(Object sender, EventArgs e)
{
    // have a function which returns the user object if successful, otherwise return null
    User user = DoLogin(txtUsername.Text, txtPassword.Text);
    if (user != null)
    {
        Session["UserRole"] = user.RoleName;
        // if you aren't using the authentication stuff from the web.config then
        // then you will need to manually redirect the user
    }   
}

home.aspx

void Page_Load(Object sender, EventArgs e)
{
    string role = !String.IsNullOrEmpty(Session["UserRole"]) ? (string)Session["UserRole"] : String.Empty;
    if (role == "ADMIN")
        Response.Redirect("adminpanel.aspx");
}

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