ASP.NET MVC 6自定义用户身份,长时间保持登录用户?

时间:2016-05-17 11:51:53

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

所以我拥有带有自定义用户身份(不使用EF)的ASP.NET MVC 6 + AngularJS应用程序,该应用程序根据不同的数据库对不同的数据库进行身份验证,等等。

我有一些用户身份工作所需的一些接口的自定义实现。

  • IUserStore<MyUser>IUserPasswordStore<MyUser> - 只为实现自定义代码的用户实施了几种方法。那里没什么有趣的。

  • IPasswordHasher<MyUser>使用我自己的代码验证散列密码以解密密码等。

  • IUserClaimsPrincipalFactory<MyUser>实现了为我自己的目的创建包含一些自定义声明的ClaimsPrincipal对象。

AccountControler进行登录和注销我使用的是:

SignInManager.PasswordSignInAsync(username, password, true, shouldLockout: false)
SignInManager.SignOut();

其中SignInManager是我自定义的 - SignInManager<MyUser>

到目前为止,一切都很顺利。

现在我在Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.TryAdd(ServiceDescriptor.Scoped<IUserStore<MyUser>, ApplicationUserStore>());
    services.TryAdd(ServiceDescriptor.Scoped<IUserPasswordStore<MyUser>, ApplicationUserStore>());
    services.TryAdd(ServiceDescriptor.Scoped<IPasswordHasher<MyUser>, ApplicationSitePasswordHasher>());
    services.TryAdd(ServiceDescriptor.Scoped<IUserClaimsPrincipalFactory<MyUser>, MyUserClaimsPrincipalFactory>());

    services.AddIdentity<MyUser, MyUserRole>();

    services.ConfigureIdentity(options =>
    {
        options.Password.RequireDigit = false;
        options.Password.RequireLowercase = false;
        options.Password.RequireNonLetterOrDigit = false;
        options.Password.RequireUppercase = false;
        options.Password.RequiredLength = 1;

        options.User.RequireUniqueEmail = false;
        options.User.UserNameValidationRegex = string.Empty;
    });

    services.ConfigureIdentityApplicationCookie(options =>
    {
        options.SlidingExpiration = true;
        options.ExpireTimeSpan = TimeSpan.FromDays(30);
        options.AuthenticationScheme = IdentityOptions.ApplicationCookieAuthenticationScheme;
        options.AutomaticAuthentication = true;
        options.LoginPath = PathString.Empty; 
    });

    ...

}

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

     app.UseIdentity();

     ...

     app.UseMvc(routes => .... )
}

当用户在角度http异步调用中获得401时,我还有自定义授权属性。这个想法是这样的:

public class MyAuthorize : ActionFilterAttribute
{
    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        bool isAuth = bool isAuth = context.HttpContext.User.Identity.IsAuthenticated;

        bool isFromClient = ...
        string loginPath = ...

        if (!isAuth && isFromClient)
        {
            ...
        }
        else if (!isAuth && !isFromClient)
        {
            context.HttpContext.Response.Redirect(loginPath);
        }
        else
        {
            await next();
        }
    }
}

问题:即使我让我的cookie在30天后过期(在这种情况下),我仍然得到一些未经身份验证(context.HttpContext.User.Identity.IsAuthenticated属性为false)由于我在MyAuthorize属性中的逻辑而被重定向到登录页面。

IIS池中的空闲超时也为0。

如何让我的自定义用户身份能够在很长时间内保留经过身份验证的用户?

1 个答案:

答案 0 :(得分:1)

“某段时间”是否“登录后30分钟”? :)

实施<?php $product_id = '14'; $product = new WC_product($product_id); $attachment_ids = $product->get_gallery_attachment_ids(); foreach( $attachment_ids as $attachment_id ) { // Display the image URL echo $Original_image_url = wp_get_attachment_url( $attachment_id ); // Display Image instead of URL echo wp_get_attachment_image($attachment_id, 'full'); } ?> 界面。

长描述位于ASP.NET 5 Identity 3 users get signed out after some time

相关问题