2016-06-08 3 views
2

내가 구글 쿠키 인증을 사용하여 작동하지 않습니다ASP.NET 핵심 웹 응용 프로그램 ExpireTimeSpan은 다음과 같은 옵션이

구글 쿠키를 통해 인증 30 분 후에 만료됩니다 후
 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); 
    } 

. 그리고 그들은 세션을 위해 만들어졌습니다.

enter image description here

나는 exparation 시간을 높이기 위해 무엇을해야합니까?

답변

2

ASP.NET 핵심 ID를 사용할 때는 app.UseIdentity()이 이미 사용하므로 자신의 쿠키 미들웨어를 사용하지 마십시오. services.AddIdentity(options => { ...}에 전화 할 때 ID 옵션에서 직접 쿠키 수명/이름/경로를 구성 할 수 있습니다.

_signInManager.ExternalLoginSignInAsyncisPersistent: true으로 지정하지 않으면 브라우저를 닫을 때 만료되는 세션 쿠키가 생성됩니다. 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 }); 
    } 
} 
+0

답장을 보내 주셔서 감사합니다. app.UseIdentity(); Startup.cs,하지만 이제는 _signInManager.GetExternalLoginInfoAsync();를 기다리고 있습니다. 항상 null이며 앱에서 승인되면 로그인 페이지로 리디렉션됩니다. 이 문제를 검색하려고했지만 해결책을 찾지 못했습니다. –

+0

Startup.cs에서 app.UseCookieAuthentication을 설명하기 위해이 문제가 해결되었습니다. 이제는 _signInManager.ExternalLoginSignInAsync을 (를) 기다리는 데 예외가 있습니다. "이 DbContext에 대해 데이터베이스 공급자가 구성되어 있지 않습니다." 나는 서비스가있다 .AddDbContext (); 및 .AddEntityFrameworkStores (); 응용 프로그램이 없으면 작동하지 않기 때문에 시작할 수 없습니다. –

+0

결국 app.UseIdentity()를 삭제했습니다. StartUp에서 추가하고 HttpContext.Authentication.SignInAsync ("MyCookieMiddlewareInstance", User, properties)를 기다립니다. 다음과 같이 ExternalLoginCallback에 : [http://i.imgur.com/fpLiiGS.png] (http://i.imgur.com/fpLiiGS.png) 그것은 나를 위해 작동합니다. –

관련 문제