2014-11-24 2 views

답변

5

가능합니다. 이를 수행하는 한 가지 방법은 쿠키가 인증 될 때마다 호출되는 OnValidateIdentity 콜백을 사용하는 것입니다.이 요청은 웹 응용 프로그램에 요청할 때마다 (활성 모드라고 가정)입니다.

var options = new CookieAuthenticationOptions 
{ 
    // usual options such as LoginPath, for example, go here... 
    LoginPath = new PathString("/Account/Login"), 
    Provider = new CookieAuthenticationProvider 
    { 
     OnValidateIdentity = context => 
     { 
      DateTimeOffset now = DateTimeOffset.UtcNow; 

      context.OwinContext.Request.Set<double>("time.Remaining", 
        context.Properties.ExpiresUtc.Value.Subtract(now).TotalSeconds); 

      return Task.FromResult<object>(null); 
     } 
    } 
}; 

app.UseCookieAuthentication(options); 

여기서 OWIN 환경 사전에 남은 초를 저장합니다. 사전에 액세스 할 수있는 곳이면 어디서든 사용할 수 있으며 사용자에게 알릴 수 있습니다. 예를 들어 MVC 컨트롤러에서 이와 같은 작업을 수행 할 수 있습니다.

[Authorize] 
public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var secondsRemaining = (double)Request.GetOwinContext() 
             .Environment["time.Remaining"]); 

     // Do what you want to do with the secondsRemaining here... 

     return View(); 
    } 
} 
+0

'OnValidateIdentity'는 쿠키가 다시 발행되기 전에 호출됩니다. 슬라이딩 세션을 사용할 때 이것은보고 된 만료 시간이 쿠키가 확장 된 요청에 대해 틀리며 후속 요청에만 맞음을 의미합니다. – Justin

관련 문제