简单会员提供商 - 实体框架和角色 [英] Simple Membership Provider - Entity Framework and Roles

查看:118
本文介绍了简单会员提供商 - 实体框架和角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将MVC 4中的简单会员资源提供者用作本书。以下是目前的情况:



- 我一直在使用Jon Galloway的博客文章,主题为这里



1)我知道这个东西是通过实体框架连接的。但是,我注意到,当我添加到UserProfile类的属性时,它们在生成时没有自动出现在表中。这是由于数据库已经生成(表不存在)?我手动添加了这些字段,它功能很好,但很高兴知道这些字段不会被自动创建。

2)到目前为止作为角色,它似乎主要面向全局许可类型的东西(即用户是用户,管理员等)。如果您希望在项目级别处理(例如project1的admin,project2的用户),需要进行什么修改?



等)是有一篇关于如何扩展它的最佳实践的文章真的详细?

解决方案

1)我找到UserProfile简单会员资格表的一部分有点复杂(在一个很好的方式),但它对我的应用程序非常有用。



简单会员资格的惯例是创建一个UserProfile表命名为UserProfile,其中包含两个字段UserId和UserName。您可以通过修改Filters / InitializeSimpleMembershipAttribute.cs中的WebSecurity.InitializeDatabaseConnection()行,为UserProfile配置不同的表名或不同的UserId和UserName字段名称。您可以创建一个带有其他字段的UserProfile表,如果简单成员资格第一次运行,则会由Simple Membership使用。在默认配置下,您的应用程序运行SimpleMembership将首次创建数据库表,包括Filters / InitializeSimpleMembershipAttribute.cs中指定的任何UserProfile表详细信息。



所以,诀窍是在首次调用Simple Membership之前创建所需的UserProfile表(包括该表中所需的所有字段)。这可以由EF迁移创建,或者由数据库脚本创建,甚至可以在SSMS中手动创建。



如果您想查看简单成员身份代码,请参阅 http://aspnetwebstack.codeplex.com/SourceControl/changeset/ view / 5cb74eb3b2f3#src / WebMatrix.WebData / WebSecurity.cs http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs



)我同意你对角色和全局权限的看法。也许你可以使用AddUsersToRoles和RemoveUsersFromRoles(在 http: //aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleRoleProvider.cs )根据他们使用的项目在登录时修改用户的角色。



etc)我不知道有关扩展Simple Membership的一篇好文章,但是原则上Simple Membership扩展了扩展成员资格提供程序,它扩展了ASP.NET Membership Provider。你应该可以在适当的位置跳入。



编辑回应罗伯特的评论:



直接回答为什么Entity Framework没有创建列添加到UserProfile类中,当在特定于应用程序的表创建运行之前,由SimpeMembership初始化创建了UserProfile表。原因是SimpleMembership具有UserProfile表的内置定义,该表在SimpleMembership创建该表时使用。 UserProfile表创建的时间很重要,因此需要确保在SimpleMembership初始化运行之前创建特定于应用程序的表。


I'm attempting to use the Simple Membership Provider with MVC 4 as "by the book" as possible. Here is the current scenario:

-- I've been using Jon Galloway's blog post on the topic here.

1) I'm aware this thing is wired via Entity Framework. I did notice, however, that when I added properties to the UserProfile class, they didn't appear in the table automatically when it was generated. Is this due to the database already being generated (tables were not present)? I manually added the fields and it was functional, but would be nice to know the "gotchas" that would result in the fields not being automatically created.

2) As far as the roles, it seems that it is geared primarily towards a global permission type thing (ie user is a User, Admin, etc.). In the event that you want to make it handle at a project level (ie admin for project1, user for project2), what modifications need to be made?

etc) Is there an article that really goes into detail in regards to best practices on how to extend it?

解决方案

1)I find the UserProfile table part of Simple Membership a little complicated (in a good way), but it has worked great for my apps.

The convention for Simple Membership is to create a UserProfile table named "UserProfile" with two fields, UserId and UserName. You can configure a different table name or different UserId and UserName field names for the UserProfile by modifying the WebSecurity.InitializeDatabaseConnection() line in Filters/InitializeSimpleMembershipAttribute.cs. You can create a UserProfile table with additional fields and it will be used by Simple Membership if Simple Membership finds that table the first time it runs. Under the default configuration, the first time your app runs SimpleMembership will create the database tables including whatever UserProfile table details specified in Filters/InitializeSimpleMembershipAttribute.cs.

So, the trick is to create the UserProfile table you want (including all the fields you want in that table) before the first call to Simple Membership. This could be created by EF Migrations or created by a database script or even created manually in SSMS.

If you want to dig into the Simple Membership code, see http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/WebSecurity.cs and http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs.

2)I agree with your point about roles and global permissions. Maybe you could use AddUsersToRoles and RemoveUsersFromRoles (in http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleRoleProvider.cs) to modify a user's roles at login according to the project they use.

etc)I don't know of a good article on extending Simple Membership, but in principle Simple Membership extends the Extended Membership Provider which extends ASP.NET Membership Provider. You should be able to jump in at an appropriate point.

EDIT in response to Robert's comment:

As a direct answer to why Entity Framework did not create the columns added to the UserProfile class, this happens when the UserProfile table was already created by the SimpeMembership initialization before the app-specific table creation ran. The reason is SimpleMembership has an inbuilt definition of the UserProfile table that is used anytime SimpleMembership creates that table. The timing of the UserProfile table creation is important, so there is a need to make sure the app-specific tables are created before the SimpleMembership initialization runs.

这篇关于简单会员提供商 - 实体框架和角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆