我是ASP.NET的新手,目前正在学习ASP.NET身份.我知道它是建立在微软的owin实现之上的,我也在学习这一点.因此,我在owin启动代码中遇到了扩展方法CreatePerOwinContext,我没有看到使用它的明确目的.它是某种依赖注入容器吗?这个方法的真正目的是什么?在什么情况下应该适用呢?

推荐答案

CreatePerOwinContext注册一个静态回调,应用程序将使用该回调返回指定类型的新实例

假设您已经定义了自己的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)
{
    ...
}

Now you will be able to access your database context 和 user manager in a simple straightforward way:

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>();

Asp.net相关问答推荐

为什么 @Html.EditorFor 和 @Html.PasswordFor 在 MVC 中创建不同的样式框?

jQuery隐藏字段

业务逻辑中的实体框架最佳实践?

解析器错误消息:文件/TestSite/Default.aspx.cs不存在

System.Net.Http 版本冲突导致构建警告

即使使用正确的 Accepts 标头,WebAPI 也不会返回 XML

如何从网页 (asp.net) 启动 EXE

在 C# 中将 IHtmlContent/TagBuilder 转换为字符串

IIS 6.0 通配符映射基准?

如何配置 ASP.NET Core 1.0 以使用本地 IIS 而不是 IIS Express?

如何从 HTTP 请求中获取 MAC 地址?

如何在 IIS 7.5 上使用 ASP.NET 表单身份验证保护静态文件?

如何确定服务器端 C# 上的浏览​​器窗口大小

在 ASP.NET 中实现 404 的最佳方法

如何在 asp net core api 中使用 Created(或 CreatedAtAction / CreatedAtRoute)

如何使用 WebRequest 发布数据并从网页获取响应

在c#中显示带有换行符的标签文本

ASP.NET 核心,更改未经授权的默认重定向

判断 IQueryable 结果集的最佳方法是什么?

您的下一个 ASP.NET 项目的 Select 是什么:Web 窗体还是 MVC?