2014-04-08 1 views
14

사용자가 웹 사이트에 액세스하고 데이터베이스에 저장된 자격 증명을 입력하면 인증을 만들 때 우리는 인증을 만듭니다.MVC 5에서 OwinContext의 TimeOut을 설정하는 방법

어떻게 타임 아웃을 설정합니까? MVC 5. 사용

내 인증은 다음과 같습니다

// Enable the application to use a cookie to store information for the signed in user 
app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/Account/Login"), 
    ExpireTimeSpan = TimeSpan.FromDays(2) 
}); 
:
 var claims = new List<Claim>(); 
     claims.Add(new Claim("UserId", user.UserID.ToString())); 
     claims.Add(new Claim(ClaimTypes.Name, user.FirstName + " " + user.LastName)); 
     claims.Add(new Claim(ClaimTypes.Email, user.Email)); 
     claims.Add(new Claim(ClaimTypes.NameIdentifier, user.UserID.ToString())); 
     var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

     var ctx = Request.GetOwinContext(); 
     var authenticationManager = ctx.Authentication; 
     authenticationManager.SignIn(id); 

답변

22

고정 만료 시간 범위를 설정하는 방법이처럼 Startup.Auth.cs 파일에 ExpireTimeSpan 속성을 설정하는 것입니다

쿠키가 지속되도록 설정해야합니다. 코드에서는 사용자 이름과 암호에 추가 부울에 통과해야하고

authenticationManager.SignIn(id); 

은 당신이 시작을 사용할 필요가 없습니다 다음으로

authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, id); 
+0

수정되었지만 수정이 필요합니다. authenticationManager.SignIn (새 AuthenticationProperties {IsPersistent = true}, id); – DavidJS

+0

다행입니다. 질문에 답변으로 표시하십시오. – Dave

+0

이게 다행스럽게도 나에게 효과가 없나요? –

4

을로 변경됩니다. cs

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddHours(1), }, id); 
+0

내 TimeOut이 다른 사용자마다 다르므로이 기능을 사용하면 도움이됩니다. –