Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录站点后重定向_C#_Asp.net_Web Applications_Redirect_Login - Fatal编程技术网

C# 登录站点后重定向

C# 登录站点后重定向,c#,asp.net,web-applications,redirect,login,C#,Asp.net,Web Applications,Redirect,Login,我在web应用程序(asp.NET4)中使用asp登录控件。如果用户处于管理员角色,我希望重定向到管理员页面 我使用此代码,但不起作用: protected void baseLogin1_LoggingIn(object sender, LoginCancelEventArgs e) { if (Page.User.Identity.IsAuthenticated && Roles.IsUserInRole(Page.User.Identity.Name

我在web应用程序(asp.NET4)中使用asp登录控件。如果用户处于管理员角色,我希望重定向到管理员页面

我使用此代码,但不起作用:

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");
        }
    }

请帮助我。

在对用户进行身份验证之前引发LoggineEvent。所以你的条件的第一部分总是错误的。将逻辑移到LoggedIn事件下。 试试这个:

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

使用LoggedIn事件:事件描述为

您应该真正使用
Server.Transfer(“~/admin/Default.aspx”)因为它效率更高(往返次数更少)


如果页面需要保留书签的查询字符串,或者在浏览器中保存正确的URL很重要,则需要
Response.Redirect()
,但要注意额外的带宽成本。

page.User.Identity.IsAuthenticated和Roles.IsUserInRole(page.User.Identity.Name,“Admin”)不工作。当用户提交登录信息但在网站上对用户进行身份验证之前,会引发LoggingIn事件。在验证用户之前,使用Logginging事件设置所需的任何信息。通过将CancelEventArgs对象的cancel属性设置为true,可以在Logginging事件期间取消登录尝试。引发LoggingIn事件后,Login控件引发Authenticate事件,然后引发LoggedIn事件。