简体   繁体   English

建立用户角色之前登录后的ASP.NET Identity重定向

[英]ASP.NET Identity redirecting after login before user roles are established

My login is pretty much Identity standard code. 我的登录信息几乎是Identity标准代码。 I've added a few things to do what I need but for the most part the login code remains unedited: 我添加了一些东西来做我需要的事情,但是在大多数情况下,登录代码仍未编辑:

var result = signinManager.PasswordSignIn(User.UserName, Password.Text, RememberMe.Checked, shouldLockout: false);

switch (result)
{
    case SignInStatus.Success:
        // Store the user's supplier Id (if it exists - i.e. user is not an admin).
        using (ApplicationDbContext Context = new ApplicationDbContext())
        {
            Session["UserId"] = User.Id;
        }

        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
        break;
}

I modified the redirect in IdentityHelper to take the user here or there depending on his role: 我在IdentityHelper修改了重定向,以根据其角色将用户带到此处或那里:

public static void RedirectToReturnUrl(string returnUrl, HttpResponse response)
{
    if (!String.IsNullOrEmpty(returnUrl) && IsLocalUrl(returnUrl))
    {
        response.Redirect(returnUrl);
    }
    else
    {
        if (HttpContext.Current.User.IsInRole("Supplier"))
            response.Redirect("~/Members/Supplier.aspx");

        if (HttpContext.Current.User.IsInRole("Admin"))
            response.Redirect("~/Members/Admin.aspx");

        if (HttpContext.Current.User.IsInRole("Consultant"))
            response.Redirect("~/Members/Consultant.aspx");
    }
}

The problem is, the first time I log in with a new user (ie I logged in as an admin, logged out and tried to log in as a supplier), the login doesn't redirect because HttpContext.Current.User.IsInRole("Supplier") returns false. 问题是,当我第一次以新用户身份登录(即我以管理员身份登录,注销并尝试以供应商身份登录)时,由于HttpContext.Current.User.IsInRole("Supplier")返回false。

If I immediately try again with the same login credentials, it works perfectly fine so it seems to me that the system is attempting the redirect too soon and the login hasn't actually properly compiled the user information. 如果我立即使用相同的登录凭据再次尝试,则可以正常工作,因此在我看来,系统尝试重定向的时间过早,而登录实际上并未正确编译用户信息。

If this is the case, what would be the best way to slow it down so that it only attempts the redirect after the user has been properly allocated? 如果是这种情况,减慢速度以便仅在正确分配用户后才尝试重定向的最佳方法是什么?

I feel like a simple do...while loop might work well enough: 我觉得一个简单的do...while循环可能效果很好:

do
{
    // Literally do nothing while the condition is true.
}
while (!HttpContext.Current.User.IsAuthenticated);

Seems a bit hackish though... 不过似乎有点骇人听闻...

Identity information store in a cookie. 身份信息存储在cookie中。 In your scenario, cookie has not created yet, so that HttpContext.Current.User does not included identity information. 在您的方案中,cookie尚未创建,因此HttpContext.Current.User不包含身份信息。

Having do while loop does not fix your issue. do while循环无法解决您的问题。

Get user role and relevant data from database using userId and redirect users according to there role rather than trying to get that from HttpContext . 使用userId从数据库中获取用户角色和相关数据,并根据那里的角色重定向用户,而不是尝试从HttpContext获取用户角色和相关数据。

You can achieve this using UserManager.IsInRoleAsync(user.Id,"Admin") . 您可以使用UserManager.IsInRoleAsync(user.Id,"Admin")

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

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