Asp.Net MVC核心和身份3

时间:2016-02-29 09:30:26

标签: c# asp.net asp.net-core-mvc asp.net-identity-3

我使用vnext / core 1.0进行了网站设置,使用Identity 3进行身份验证。我可以创建用户,我可以更改密码,我可以正常登录。问题是,它似乎忽略了ExpireTimespan属性,因为我经过一段时间后随机被踢出了应用程序,并且我正在努力找到它的底部。

我有自己的用户名和用户管理员

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");
                }                   
            }

1 个答案:

答案 0 :(得分:0)

在这里查看我的问题: ASP.NET Core 1.0 - MVC 6 - Cookie Expiration

我遇到了同样的问题,花了好几个小时在github上浏览aspnet Identity的操作系统代码: - )

您的自定义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;
    }