SimpleMembership
最近2个月以来,一直在学习MVC,从最开始的2,一直到最新的4.从原来的aspx到现在的Razor引擎,越学越开心,越学越上瘾。
最近为新项目做准备,打算用MVC4,VS2012+SQL2012,反正啥新用啥。
新技术虽然方便,但也会遇到以往没有的问题,这不SimpleMembership在这里就出现了。
以下为学习SimpleMembership中遇到的一些问题及解决办法。
以前在webform中一直用Membership,MVC2,3中也一样使用,没啥差别,可这次不同了,Mvc4中突现SimpleMembership,初遇,百度一翻,觉得挺爽,功能基本和原来差不多,可以自动帮我建表。
但问题也来了,我不单要管理用户,还要管理角色。可角色管理网上却没查到相关资料。
直到今天和湿哥一起研究,又百度,又查查查,终于被我查到一篇英文文档,如下:
First, the new project template creates an MVC 4 Internet application without any provider configuration, but for the membership features to work properly during a migration, it appears you need at least some configuration. The following code makes sure the SimpleMembershipProvider and SimpleRolesProvider are in place.
<roleManager enabled="true" defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,
WebMatrix.WebData"/>
</providers>
</roleManager>
<membership defaultProvider="simple">
<providers>
<clear/>
<add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,
WebMatrix.WebData"/>
</providers>
</membership>Then inside the Seed method of the DbMigrationsConfiguration<T> derived class, you can have:
protected override void Seed(MovieDb context)
{
//context.Movies.AddOrUpdate(...);
// ...
SeedMembership();
}
private void SeedMembership()
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection",
"UserProfile", "UserId", "UserName", autoCreateTables: true);
var roles = (SimpleRoleProvider) Roles.Provider;
var membership = (SimpleMembershipProvider) Membership.Provider;
if (!roles.RoleExists("Admin"))
{
roles.CreateRole("Admin");
}
if (membership.GetUser("sallen",false) == null)
{
membership.CreateUserAndAccount("sallen", "imalittleteapot");
}
if (!roles.GetRolesForUser("sallen").Contains("Admin"))
{
roles.AddUsersToRoles(new[] {"sallen"}, new[] {"admin"});
}
}
配置照做,看到SeedMembership与项目中的InitializeSimpleMembershipAttribute中的SimpleMembershipInitializer比较相像,就结合了一下
private class SimpleMembershipInitializer { public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); //----------------- var roles = (SimpleRoleProvider) Roles.Provider; if (!roles.RoleExists("Admin")) { roles.CreateRole("Admin"); } //----------------- } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } } }
结果成功创建Admin Role。
湿哥说,可以不用配置,直接使用,试验之,成功。
结果表明,使用SimpleMembership确实非常方法,好多都集成了,不用再像以前那样运行向导再生成了。
但可惜的是,SimpleMembership的扩展只支持UserId为int类型,有点不爽,我想使用GUID,有点局限了。
上张图,来帮助理解实现过程(2013-11-28):
SimpleMembership续
一些学习资料:
Asp.net Mvc4默认权限详细(上)
Asp.net Mvc4默认权限详细(下)
http://www.codeproject.com/Articles/637428/SimpleMembershipProvider-vs-MembershipProvider
http://www.codeguru.com/csharp/.net/net_asp/mvc/using-simplemembership-in-asp.net-mvc-4.htm