简体   繁体   中英

Redirect after login in site

i use asp login control in web application (ASP.NET 4). if user in admin role i want redirect to admin page.

i use this code, but not working:

protected void baseLogin1_LoggingIn(object sender, LoginCancelEventArgs e)
    {
        if (Page.User.Identity.IsAuthenticated && Roles.IsUserInRole(Page.User.Identity.Name, "Admin"))
        {
            Page.Response.Redirect("admin/Default.aspx");
        }
    }

please help me.

LoggingInEvent is raised before the user is authenticated. So the first part of your condition is always false. Move you logics under LoggedIn event. Try this one:

protected void baseLogin1_LoggedIn(object sender, EventArgs e)
{
    if (Context.User.Identity.IsAuthenticated && Context.User.IsInRole("Admin"))
    {
        Context.Response.Redirect("admin/Default.aspx");
    }
}

Use the LoggedIn event: Event description is here

You should really be using Server.Transfer("~/admin/Default.aspx"); as it is a little more efficient (less round trips).

If the page needs to preserve the query string for a bookmark or it is important to preseve the correct URL in the browser then Response.Redirect() is needed but be aware of the extra bandwidth cost.

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