2016-05-07 25 views
14

Tôi sử dụng Asp.Net Identity để kiểm soát ủy quyền của ứng dụng của tôi. Bây giờ, tôi cần thực hiện việc này: nếu người dùng không hoạt động sau 30 phút, hãy chuyển đến trang đăng nhập, anh ta đăng nhập không chọn hộp kiểm "isPersistent". Và nếu chọn hộp kiểm "isPersistent", hãy đặt ngày hết hạn của cookie trong 14 ngày. tôi cố gắng làm điều này bằng cách thay đổi các Starup.Auth.cs như thế này:Làm thế nào để thiết lập asp.net Identity cookies hết hạn thời gian

public void ConfigureAuth(IAppBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login"), 
     SlidingExpiration = true, 
     CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME 
    }); 
} 

và mã đăng nhập như thế này:

private async Task SignInAsync(User user, bool isPersistent) 
{ 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); 
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); 
    if (isPersistent) 
    { 
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity); 
    } 
    else 
    { 
     AuthenticationManager.SignIn(new AuthenticationProperties() { ExpiresUtc = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30)) }, identity); 
    } 
} 

Nhưng tôi phát hiện ra rằng khi người dùng không chọn hộp kiểm isPersistent , ngày hết hạn của cookie đã là 'Phiên', không phải là thời gian hiện tại cộng thêm 30 phút. enter image description here

Các cookie trạng thái khi sử dụng mã như sau, do đó 'nhớ đến tôi' hộp kiểm có thể không hoạt động :(..

 app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      LoginPath = new PathString("/Account/Login"), 
      ExpireTimeSpan = TimeSpan.FromMinutes(30), 
      SlidingExpiration = true, 
      CookieName = WebHelpers.ConstStrings.AUTHCOOKIESNAME 
     }); 

enter image description here

Trả lời

21

Nếu IsPersistent thuộc tính của AuthenticationProperties được đặt thành false, thì thời gian hết hạn cookie là s et to Session.

Nếu hộp kiểm "nhớ đến tôi" là kiểm tra sau đó AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true }, userIdentity); sẽ tạo ra một cookie với thời gian hết hạn bằng ExpireTimeSpan bạn thiết lập trong Startup.cs (mặc định là 14days).

Nếu hộp kiểm "nhớ đến tôi" được KHÔNG kiểm tra sau đó bạn phải sử dụng AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);. Một lần nữa, IsPersistent được đặt thành true nhưng bây giờ chúng tôi đưa ra giá trị cho ExpiresUtc để nó không sử dụng từ CookieAuthenticationOptions từ Startup.cs.

public override async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser) 
{ 
    var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture(); 
    // Clear any partial cookies from external or two factor partial sign ins 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); 
    if (rememberBrowser) 
    { 
     var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(ConvertIdToString(user.Id)); 
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity); 
    } 
    else 
    { 
     //AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity); 
     if (isPersistent) 
     { 
      AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity); 
     } 
     else 
     { 
      AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30) }, userIdentity); 
     }   
    } 
} 
+0

Hoàn hảo, tôi sử dụng mã của bạn và đạt được chức năng trên, cảm ơn bạn. Ngoài ra, tôi đặt Owin từ phiên bản 2.1.0 để nâng cấp lên 3.0.1. –

+0

Cảm ơn, đã làm mọi thứ rõ ràng với tôi – Janiiik

+0

Cảm ơn, mã và giải thích của bạn hoạt động hoàn hảo. –

4

Sử dụng này ...

public void ConfigureAuth(IAppBuilder app) 
{ 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     ExpireTimeSpan = TimeSpan.FromHours(1), 
    });    
} 
+0

Cảm ơn, nhưng tôi phát hiện ra rằng 'nhớ đến tôi' sẽ không thể làm việc , nếu tôi đặt thuộc tính này. –

+0

Điều này không hiệu quả đối với tôi. Nó vẫn mặc định là 14 ngày. – Garth

+1

@Garth cố gắng trước tiên xóa cookie của trình duyệt của bạn theo cách thủ công. –

0

tôi đã cùng một vấn đề và mã này làm việc cho tôi (bên trong các tập tin Startup.cs) ..

services.Configure<IdentityOptions>(options => 
{ 
    options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(9999); 
}); 

này cho biết thêm khoảng 27 năm (hoặc không bao giờ hết hạn) cho dai dẳng bánh quy.

NB: Nếu bạn muốn ít hơn của một thời gian hết hạn bạn có thể sử dụng TimeSpan.FromMinutes(1); trong 1 phút hoặc TimeSpan.FromSeconds(30); trong 30 giây vv ..

Các vấn đề liên quan