首先,你必须定义模型的 - 因为你正在做的 - 实施正确的接口
比方说,你要创建一个用户,您的应用程序:
public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim>
{
public string CompanyName { get; set; }
}
正如你可以看到我已经实现了IdentityUser
接口(命名空间Microsoft.AspNet.Identity.EntityFramework)。
我指定我想用我的主键(串)什么类型的标识符和包括我的自定义对象管理登录,角色a索赔。
现在,我们可以定义角色对象:
public class MyRole : IdentityRole<string, MyUserRole>
{
}
同样有一个类型,我属于一个角色的用户管理定义的类。
public class MyUserRole : IdentityUserRole<string>
{
}
MyUserLogin
是要实现IdentityUserLogin<string>
。
MyUserClaim
即将执行IdentityUserClaim<string>
。
正如你所看到的,每个接口都需要一个主键类型。
第二步是建立用户存储:
public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyUserStore(MyContext context)
: base(context)
{
}
}
我们再次定义了什么用户,角色,登录等等等等,我们要使用。
我们需要UserStore
导致我们的UserManager需要一个。
如果您打算管理角色并将角色与每个用户关联,您必须创建您的RoleStore
定义。
public class MyRoleStore : RoleStore<MyRole, string, MyUserRole>
{
public DaufRoleStore(ApplicationDatabaseContext context) : base(context)
{
}
}
现在您可以创建UserManager
。 UserManager是保存UserStore
更改的真实responsible。
public class ApplicationUserManager : UserManager<MyUser, string>
{
public ApplicationUserManager(IUserStore<MyUser, string> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>()));
manager.UserValidator = new UserValidator<MyUser, string>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 5,
RequireNonLetterOrDigit = false, // true
// RequireDigit = true,
RequireLowercase = false,
RequireUppercase = false,
};
return (manager);
}
}
该类有一个静态方法,它将为您创建一个新的UserManager。
有趣的是,你可以包括一些验证规则,你可能需要验证密码等。
最后一件事是创建或数据库上下文。
public class MyContext : IdentityDbContext<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim>
{
public MyContext(): base("<your connection string here>")
{
}
public static MyContext Create()
{
return new MyContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyUser>()
.ToTable("Users");
modelBuilder.Entity<MyRole>()
.ToTable("Roles");
modelBuilder.Entity<MyUserRole>()
.ToTable("UserRoles");
modelBuilder.Entity<MyUserClaim>()
.ToTable("UserClaims");
modelBuilder.Entity<MyUserLogin>()
.ToTable("UserLogins");
}
}
正如您所看到的,我已经使用模型构建器来更改所有表的名称。 您可以在此处定义键或字段类型或表关系。
这是你要附上你想在你的背景来管理您的自定义类的地方:
public DbSet<MyCustomer> Customers{ get; set; }
再次MyContext
有Create
方法返回一个新的上下文:
public static MyContext Create()
{
return new MyContext();
}
现在你应该有一个启动课,你要引导你的东西:
[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))]
namespace ASPNETIdentity2
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(MyContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
}
}
}
这里您将创建您的数据库上下文以及您可以在应用程序中使用的用户管理器。
通知的第一行:
[assembly: OwinStartup(typeof(ASPNETIdentity2.Startup))]
原因你告诉你的环境是启动类需要在...启动被称为这是必要的。
现在在你的控制器,你可以简单地参考您的UserManager
做这样的事情:
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
你怎么能创建表?
在Visual Studio中,转至工具 - > NuGet Packager Manager - >程序包管理器控制台。
在窗口中有一个组合框“默认项目”。选择你的ASP.NET MVC项目。
运行以下命令:
Enable-Migrations
它会在一个名为Migrations
新文件夹创建一个文件Configuration.cs
。
如果你想创建你的数据库,你需要打开文件并更改AutomaticMigrationsEnabled
为true:
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
再次,从Package Manager Console
,你可以运行:
Update-Database
和所有的表会出现在你的数据库中。不要忘记你的连接字符串。
你可以下载这个github project看看一切正常。
您可以使用其他信息检查这些twoanswers。
两者的first已获得一些链接到一个博客,你可以了解所有这些事情。
注:
你要做到这一切,如果你想定制你的环境的每一个位。
哇。谢谢。你所放的细节是可以理解的,我真的很感谢你的努力。它很简洁,但完全描述了所有的事情。祝你有美好的一天。 –
很高兴我以某种方式提供帮助。祝你好运。我不是特别喜欢所有这些烂摊子,但这是它的工作方式。干杯。 – LeftyX