2015-01-29 2 views
1

템플릿에서 다소 수정 된 로그인을 사용하여 표준 MVC5 웹 앱을 가지고 있습니다.클레임 기반 인증에서 쿠키의 만료 설정

임 내가 여기

로그인 할 때 생성되는 쿠키에 30 분 만료를 설정하려고하는 것은

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     var user = AccountDomain.CheckUserLogin(model.UserName, model.Password); 

     if (user != null) 
     { 
      AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 

      var claims = new List<Claim> 
      { 
       new Claim("UserName", user.UserName), 
       new Claim("FirstName", user.FirstName ?? ""), 
       new Claim("LastName", user.LastName ?? "") 
      }; 

      var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

      var claimsPrincipal = new ClaimsPrincipal(identity); 

      Thread.CurrentPrincipal = claimsPrincipal; 

      AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, identity); 

      return RedirectToLocal(returnUrl); 
     } 
     else 
     { 
      ModelState.AddModelError("", "Invalid username or password."); 
     } 

     return View(model); 
    } 

내가이

var exp = new DateTimeOffset().AddMinutes(5); 
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe, ExpiresUtc = exp }, identity); 

하지만 일을 시도 내 로그인 작업입니다 쿠키 상태 만료 : 브라우징 세션이 끝날 때

'reme mber me '가 로그인 페이지에서 선택되면 IsPersistent가 true가되고 로그인 시간으로부터 14 일로 쿠키 만료를 설정합니다.

어떻게 쿠키의 만료 시간을 수동으로 설정할 수 있습니까?

app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      Provider = new CookieAuthenticationProvider 
      { 
       ExpireTimeSpan = TimeSpan.FromDays(5), 
       SlidingExpiration = true 
      } 
     }); 

ExpireTimeSpan 당신에게 수동으로 만료 시간을 설정할 수있는의 abillity을 제공 :

답변

0

당신은 다음 코드를 StartUp.cs 설정 파일이 있어야합니다.

+0

ExpireTimeSpan은 제공자 외부에 있어야합니다. – Rob

0

ExpireTimeSpan은 영구 로그인의 만료를 설정합니다. 그러나 두 가지 유형의 로그인을 모두 지원하려는 경우 원하는 것은 아닙니다. 다음은 정상 로그인을 위해 작동하고 영구적 인 문제를 해결하는 솔루션입니다. User logout after non-persistent login in Asp.Net Identity 2