成功验证后,站点重定向到/ Login方法

时间:2022-06-02 00:26:15

Im developing an MVC 5.0 .ASP NET app. Im using Identity to authorize users. I've added another method to AccountController where user can log in with Token. After 'signing in' in my method instead of rendering chosen view application redirects to default login method. When you type mysite/home/ it shows that user is logged in. I can't find when the redirect to Login is made. Any ideas how to fix this?

我正在开发一个MVC 5.0 .ASP NET应用程序。我使用Identity来授权用户。我在AccountController中添加了另一种方法,用户可以使用Token登录。在我的方法中“登录”而不是渲染选定的视图应用程序后重定向到默认登录方法。当您键入mysite / home /时,它显示用户已登录。我无法找到重定向到Login的时间。任何想法如何解决这一问题?

To authenticate user in my action I use this method:

要在我的操作中验证用户,我使用以下方法:

   private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }

My custom login action

我的自定义登录操作

[AllowAnonymous]
public async Task<ActionResult> LoginWithToken(string token, string returnUrl)
    {
        AccountService accountService = new AccountService(db);
        string userId;
        try
        {
            userId = await accountService.GetUserIdFromToken(token);
        }
        catch (Exception exception)
        {

            ModelState.AddModelError(exception.Message, exception);
            return RedirectToAction("Login");
        }

    var user = await UserManager.FindByIdAsync(userId);

    try
    {
        await SignInAsync(user, false);

    }
    catch (Exception)
    {
        ModelState.AddModelError("Logowanie nieudane. Konto mogło zostać zablokowane lub usunięte. Skontaktuj się  z administratorem serwisu.", new AccessViolationException());

        return RedirectToAction("Login");
    }

    LogUserLogin(user.UserName, Request.UserHostAddress);

    string decodedUrl = "";
    if (!string.IsNullOrEmpty(returnUrl))
        decodedUrl = Server.UrlDecode(returnUrl);



    if (Url.IsLocalUrl(decodedUrl))
    {
        return View((object)decodedUrl);
    }
    else
    {
        return RedirectToAction("Home", "Index", new { });
    }

}

EDIT: I've found out that when my method is hitting return View((object)decodedUrl); request doesn't contains ".AspNet.ApplicationCookie". I've set breakpoint on Application_BeginRequest and right after renturn View((object)decodedUrl) there is request to Login action. At this point there is ".AspNet.ApplicationCookie" in request.

编辑:我发现当我的方法击中返回View((object)decodingUrl);请求不包含“.AspNet.ApplicationCookie”。我已经在Application_BeginRequest上设置了断点,并且在renturn View((object)decodingUrl之后)有请求登录操作。此时请求中有“.AspNet.ApplicationCookie”。

1 个解决方案

#1


0  

Finally I've found that the authentication cookie wasn't set to the current request but to next request. Now my LoginWithToken Method redirects to another [AllowAnonymous] Action so the cookie is set. Then that action redirects to authorisation restricted areas.

最后,我发现身份验证cookie未设置为当前请求,而是设置为下一个请求。现在我的LoginWithToken方法重定向到另一个[AllowAnonymous] Action,因此设置了cookie。然后该操作重定向到授权限制区域。

#1


0  

Finally I've found that the authentication cookie wasn't set to the current request but to next request. Now my LoginWithToken Method redirects to another [AllowAnonymous] Action so the cookie is set. Then that action redirects to authorisation restricted areas.

最后,我发现身份验证cookie未设置为当前请求,而是设置为下一个请求。现在我的LoginWithToken方法重定向到另一个[AllowAnonymous] Action,因此设置了cookie。然后该操作重定向到授权限制区域。