Microsoft在OWIN中实施扩展方法CreatePerOwinContext的目的是什么

下一位开发人员:

我是ASP.NET的新手,目前正在学习ASP.NET身份。我知道它是建立在Microsoft OWIN实施之上的,我也仍然在学习。因此,我在Owin启动代码中遇到了扩展方法CreatePerOwinContext,但我看不出使用它的明确目的。它是某种依赖注入容器吗?该方法的真正目的是什么?在什么情况下应该使用它?

LeftyX:

CreatePerOwinContext注册一个静态回调,您的应用程序将使用该回调来获取指定类型的新实例。
每个请求将调用一次该回调,并将该对象存储在OwinContext中,以便您可以在整个应用程序中使用它们。

假设您已经定义了自己的IdentityDbContext实现

public class ApplicationDatabaseContext : IdentityDbContext<MyApplicationUser, MyRole, Guid, MyUserLogin, MyUserRole, MyUserClaim>
{
    public ApplicationDatabaseContext() : base("<connection string>")
    {
    }

    public static ApplicationDatabaseContext Create()
    {
        return new ApplicationDatabaseContext();
    }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
        base.OnModelCreating(modelBuilder);

        // Customize your table creation here.

            #region USERS - INFOS

        modelBuilder.Entity<UserInfo>()
            .Property(p => p.FirstName)
            .HasColumnType("varchar")
            .HasMaxLength(70);

        modelBuilder.Entity<UserInfo>()
            .Property(p => p.LastName)
            .HasColumnType("varchar")
            .HasMaxLength(70);

        modelBuilder.Entity<UserInfo>()
            .Property(p => p.Address)
            .HasColumnType("varchar")
            .HasMaxLength(100);

        modelBuilder.Entity<UserInfo>()
            .Property(p => p.City)
            .HasColumnType("varchar")
            .HasMaxLength(100);

        modelBuilder.Entity<UserInfo>()
            .ToTable("UsersInfo");

        #endregion  
        }

        public DbSet<UserInfo> UsersInfo { get; set; }
}

和您的UserManager实现

public class ApplicationUserManager : UserManager<MyApplicationUser, Guid>
{
    public ApplicationUserManager(IUserStore<MyApplicationUser, Guid> store) : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new MyUserStore(context.Get<ApplicationDatabaseContext>()));

            manager.UserValidator = new UserValidator<MyApplicationUser, Guid>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            manager.PasswordValidator = new PasswordValidator()
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = false,    
                // RequireDigit = true,
                RequireLowercase = false,
                RequireUppercase = false,
            };

            var dataProtectionProvider = options.DataProtectionProvider;

            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<MyApplicationUser, Guid>(dataProtectionProvider.Create("PasswordReset"));
            }

            return (manager);
        }
}

在您的Owin Startup中,您将注册回调:

// IAppBuilder app

app.CreatePerOwinContext<ApplicationDatabaseContext>(ApplicationDatabaseContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

它将调用静态方法:

public static ApplicationDatabaseContext Create()
{
    return new ApplicationDatabaseContext();
}

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
    ...
}

现在,您将能够以简单明了的方式访问数据库上下文和用户管理器:

ApplicationDatabaseContext dbContext = context.OwinContext.Get<ApplicationDatabaseContext>();
ApplicationUserManager userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

在您的ApiController中(如果您使用的是WebApi):

IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
ApplicationUserManager applicationUserManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

“?扩展A”的目的是什么?

.ZFSendToTarget扩展的目的是什么?

Firefox中的Ubuntu Online Accounts扩展的目的是什么?

AuthorizationServerSecurityConfigurer中的领域方法的目的是什么?

Java中的hashcode方法的目的是什么?

RestTemplate中交换方法的目的是什么?

PySpark中的别名方法的目的是什么?

在iOS应用中实施MVC模式的正确方法是什么?

Laravel中的Request类中的authorize方法的目的是什么?

Dagger 2中的组件中的getter方法的目的是什么?

Microsoft.Net.Compilers的目的是什么?

类方法的目的是什么?

ProtoMessage方法的目的是什么?

实施控制的最佳方法是什么

实施CupertinoActionSheet的正确方法是什么?

indexedDB get()方法中“ Number()”的目的是什么

在Rails测试(MiniTest)中,assign方法的目的是什么?

限制方法中泛型的目的是什么?

在pypi中重命名项目的推荐方法是什么?

Scala中的双方法括号的目的是什么?

javascript中“构造函数”方法的目的是什么?

fromString 方法中 BitSet valueOf 的目的是什么

KeyValuePair <>结构中的Deconstruct方法的目的是什么?

在Ruby中绑定/解除绑定方法的目的是什么?

在git中管理多个项目的最佳方法是什么?

在Angular中嵌套项目的最佳方法是什么

Rails渲染方法中的partial属性的目的是什么?

在Java中,netbeans init(); 用的方法,目的是什么?

Java中受保护的变量和方法的目的是什么