在没有实体框架和迁移的情况下,在ASP.NET Core MVC应用程序中使用ASP.NET身份



是否可以在没有实体框架和实体框架迁移的情况下使用ASP.NET身份?我的其余应用程序将使用Micro ORM进行数据访问。但是,该应用程序使用的是Inn asp.net身份单个用户帐户。

我的目标是仍然能够使用内置的UserManager和LoginManager类,并使用Micro Orm检索用户列表,并消除与EF/Migrations有关的任何事情。这可能吗?似乎不是因为原始数据库结构是通过应用初始迁移而创建的。

如果某人对此有很好的技术,请分享。

首先您需要创建自定义用户商店:

public class UserStore : IUserStore<IdentityUser>,
                         IUserClaimStore<IdentityUser>,
                         IUserLoginStore<IdentityUser>,
                         IUserRoleStore<IdentityUser>,
                         IUserPasswordStore<IdentityUser>,
                         IUserSecurityStampStore<IdentityUser>
{
    // interface implementations not shown
}

然后您需要将其注册到依赖项注入容器中:

// Add identity types
services.AddIdentity<ApplicationUser, ApplicationRole>()
    .AddDefaultTokenProviders();
// Identity Services
services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>();
services.AddTransient<IRoleStore<ApplicationRole>, CustomRoleStore>();

这是在此处记录的。

asp.net身份已经在其商店中提取了所需的商店和文档;
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/indentity-custom-storage-providers

这是商店的一个例子;

public class InMemoryUserStore<TUser> :
        IUserStore<TUser>,
        IUserLoginStore<TUser>,
        IUserClaimStore<TUser>,
        IUserPasswordStore<TUser>,
        IUserSecurityStampStore<TUser>,
        IUserTwoFactorStore<TUser>,
        IUserEmailStore<TUser>,
        IUserLockoutStore<TUser>,
        IUserAuthenticatorKeyStore<TUser>,
        IUserTwoFactorRecoveryCodeStore<TUser>,
        IUserPhoneNumberStore<TUser> where TUser: MemoryIdentityUser
    {
        ...
    }

您也可以拥有自己的用户对象,并且它不必从任何东西继承。

public class MemoryIdentityUser
{
    private List<MemoryUserClaim> _claims;
    private List<MemoryUserLogin> _logins;
    private List<MemoryUserToken> _tokens;
    ...
}

ASP.NET身份是一种引擎,因此被认为是一种引擎。正是这种观点推动了商店的抽象。我希望ASP.NET身份文档对其与商店的交互方式具有完整的序列图。至少要兑现一些参考序列。

商店有一些怪癖,它需要使用仅突变实施中的私人数据的方法,然后进行更新调用,以假定您将数据提交持续存储。

您可能想检查这个项目;https://github.com/ghstahl/aspnetcore.2.inmemoryidentity

您可以看到如果没有数据库的负担,您需要做什么。

将其连接;

// My user is custom, so I made ApplicationUser inherit
public class ApplicationUser : MemoryIdentityUser
{
}

startup.cs;

public void ConfigureServices(IServiceCollection services)
{
  services.AddSingleton<IUserStore<ApplicationUser>>(provider =>
  {
    return new InMemoryUserStore<ApplicationUser>();
  });
  services.AddIdentity<ApplicationUser>(Configuration)
          .AddDefaultTokenProviders();
  // Add application services.
  services.AddTransient<IEmailSender, EmailSender>();
  services.AddMvc();
}

添加身份,以下说明了您可以引入自己的实现的范围

public static class InMemoryIdentityServiceCollectionExtensions
{
    public static IdentityBuilder AddIdentity<TUser>(this IServiceCollection services, IConfiguration configuration)
        where TUser : class => services.AddIdentity<TUser>(configuration,null);
    public static IdentityBuilder AddIdentity<TUser>(this IServiceCollection services, IConfiguration configuration,Action<IdentityOptions> setupAction)
        where TUser : class
    {
        // Services used by identity
        var authenticationBuilder = services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
                options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
                options.DefaultSignInScheme = IdentityConstants.ExternalScheme;
            })
            .AddCookie(IdentityConstants.ApplicationScheme, o =>
            {
                o.LoginPath = new PathString("/Account/Login");
                o.Events = new CookieAuthenticationEvents
                {
                    OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
                };
            })
            .AddCookie(IdentityConstants.ExternalScheme, o =>
            {
                o.Cookie.Name = IdentityConstants.ExternalScheme;
                o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
            })
            .AddCookie(IdentityConstants.TwoFactorRememberMeScheme,
                o => o.Cookie.Name = IdentityConstants.TwoFactorRememberMeScheme)
            .AddCookie(IdentityConstants.TwoFactorUserIdScheme, o =>
            {
                o.Cookie.Name = IdentityConstants.TwoFactorUserIdScheme;
                o.ExpireTimeSpan = TimeSpan.FromMinutes(5);
            });
        // Hosting doesn't add IHttpContextAccessor by default
        services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        // Identity services
        services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
        services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
        services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        // No interface for the error describer so we can add errors without rev'ing the interface
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser>>();
        services.TryAddScoped<UserManager<TUser>, AspNetUserManager<TUser>>();
        services.TryAddScoped<SignInManager<TUser>, SignInManager<TUser>>();
        if (setupAction != null)
        {
            services.Configure(setupAction);
        }
        return new IdentityBuilder(typeof(TUser), services);
    }
}

那里有很多iuserstore实现,其中包括每种类型的备份数据库。我从另一个使用MongoDB作为支持DB的项目中复制了我的inmemoryuserstore。

相关内容

  • 没有找到相关文章

最新更新