UseCookieAuthentication中的ASP.NET核心Web应用程序ExpireTimeSpan不起作用

时间:2016-06-08 16:15:55

标签: asp.net asp.net-core

我在Google使用Cookie身份验证时使用以下选项:

       app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AuthenticationScheme = "MyCookieMiddlewareInstance",
            LoginPath = new PathString("/Account/Login/"),
            AccessDeniedPath = new PathString("/Account/Forbidden/"),
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            ExpireTimeSpan = TimeSpan.FromDays(14.0)
        });

        app.UseGoogleAuthentication(new GoogleOptions()
        {
            SignInScheme = "MyCookieMiddlewareInstance",
            AutomaticAuthenticate = true,
            ClientId = "xxx",
            ClientSecret = "xxx"
        }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public IActionResult ExternalLogin(string provider, string returnUrl = null)
    {
        // Request a redirect to the external login provider.
        var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
        var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        return Challenge(properties, provider);
    }

    [HttpGet]
    [AllowAnonymous]
    public IActionResult ExternalLoginCallback(string returnUrl = null, string remoteError = null)
    {
        return RedirectToLocal(returnUrl);
    }

经过Google Cookie授权后,30分钟后即可过期。它们是为会话而创建的。

enter image description here

我应该怎样做才能提高排课时间?

1 个答案:

答案 0 :(得分:2)

使用ASP.NET核心标识时,不应使用自己的Cookie中间件,因为app.UseIdentity()已经为您完成了。在调用services.AddIdentity(options => { ...})时,您可以直接在Identity选项中配置cookie生存期/名称/路径。

如果您未使用_signInManager.ExternalLoginSignInAsync致电isPersistent: true,则会获得会话Cookie,该Cookie会在关闭浏览器时到期。您可以更新ExternalLoginCallback以修复该问题:

[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
{
    if (remoteError != null)
    {
        ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
        return View(nameof(Login));
    }
    var info = await _signInManager.GetExternalLoginInfoAsync();
    if (info == null)
    {
        return RedirectToAction(nameof(Login));
    }

    // Sign in the user with this external login provider if the user already has a login.
    // Specify isPersistent: true to avoid getting a session cookie.
    var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: true);
    if (result.Succeeded)
    {
        // Update any authentication tokens if login succeeded
        await _signInManager.UpdateExternalAuthenticationTokensAsync(info);

        _logger.LogInformation(5, "User logged in with {Name} provider.", info.LoginProvider);
        return RedirectToLocal(returnUrl);
    }
    if (result.RequiresTwoFactor)
    {
        return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl });
    }
    if (result.IsLockedOut)
    {
        return View("Lockout");
    }
    else
    {
        // If the user does not have an account, then ask the user to create an account.
        ViewData["ReturnUrl"] = returnUrl;
        ViewData["LoginProvider"] = info.LoginProvider;
        var email = info.Principal.FindFirstValue(ClaimTypes.Email);
        return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = email });
    }
}