2016-02-29 112 views
0

我有一個使用vnext/core 1.0的站點設置,它使用Identity 3進行身份驗證。我可以創建用戶,我可以更改密碼,我可以很好地登錄。問題是,它似乎忽略了ExpireTimespan屬性,因爲我在一段時間後被隨機踢出了應用程序,而我正在努力達到它的底部。Asp.Net MVC核心和標識3

我有我自己的userstore和的UserManager

public IServiceProvider ConfigureServices(IServiceCollection services) 
{ 
    ... 

    services.AddIdentity<Domain.Models.User, Domain.Models.UserRole>() 
       .AddUserStore<UserStore>() 
       .AddRoleStore<RoleStore>() 
       .AddUserManager<MyUserManager>()     
       .AddDefaultTokenProviders(); 

    ... 

} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
... 

app.UseMyIdentity(); 

... 
} 


public static IApplicationBuilder UseMyIdentity(this IApplicationBuilder app) 
     { 
      if (app == null) 
      { 
       throw new ArgumentNullException(nameof(app)); 
      } 

      var marker = app.ApplicationServices.GetService<IdentityMarkerService>(); 
      if (marker == null) 
      { 
       throw new InvalidOperationException("MustCallAddIdentity"); 
      } 

      var options = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>().Value; 

      app.UseCookieAuthentication(options.Cookies.ExternalCookie); 
      app.UseCookieAuthentication(options.Cookies.TwoFactorRememberMeCookie); 
      app.UseCookieAuthentication(options.Cookies.TwoFactorUserIdCookie); 
      CookieAuthenticationOptions appCookie = options.Cookies.ApplicationCookie; 

      appCookie.LoginPath = new Microsoft.AspNet.Http.PathString("/Login"); 
      appCookie.SlidingExpiration = true; 
      appCookie.ExpireTimeSpan = TimeSpan.FromHours(8); 
      appCookie.CookieName = "MyWebApp"; 

      app.UseCookieAuthentication(appCookie); 
      return app; 
     } 

登錄控制器

var user = await userManager.FindByNameAsync(model.Username); 

      if (user != null) 
      { 
       SignInResult result = await signInManager.PasswordSignInAsync(user, model.Password, false, false); 

       if (result.Succeeded) 
       { 
        RedirectToActionPermanent("Index", "Home"); 
       }     
      } 

回答

0

見我的問題在這裏:經歷了 ASP.NET Core 1.0 - MVC 6 - Cookie Expiration

我遇到了同樣的問題,花了幾個小時在github上的aspnet身份驗證的操作系統代碼:-)

您的自定義UserManager必須執行Get/UpdateSecurityStampAsync

public class MyUserManager:UserManager<MinervaUser> 
{ 
... 
    public override bool SupportsUserSecurityStamp 
    { 
     get 
     { 
      return true; 
     } 
    } 
    public override async Task<string> GetSecurityStampAsync(MinervaUser user) 
    { 
     // Todo: Implement something useful here! 
     return "Token"; 
    } 

    public override async Task<IdentityResult> UpdateSecurityStampAsync(MinervaUser user) 
    { 
     // Todo: Implement something useful here! 
     return IdentityResult.Success; 
    } 
+0

感謝回覆@mcb。我最終設置了自己的ClaimsIdentity,結果比使用身份更簡單和容易,他們在mvc6方面做得非常好:https://docs.asp.net/en/latest/security/authentication/cookie .html – Phil

+0

是的,從昨天開始,RC2就有文檔[鏈接](https://docs.asp.net/en/latest/security/authentication/cookie.html)。仍然不會介意,如果你接受答案:-) – mcb