在新的MVC 4互联网使用模板simplemembership基于角色的身份验证 [英] Role based authentication in the new MVC 4 Internet template using simplemembership

查看:268
本文介绍了在新的MVC 4互联网使用模板simplemembership基于角色的身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢与链接到OAuth在VS 2012 RTM外部登录MVC 4网络模板新simplemembership功能。在大多数情况下认证功能正在工作。然而,即使在这个花费8个多小时后,我无法实现基于角色的授权,在我的控制器工作。 SimpleMembership是谈到了是什么,但简单。

I like the new simplemembership feature in MVC 4 internet template with links to OAuth for external logins in VS 2012 RTM. For the most part authentication feature are working. However even after spending over 8 hours on this I am unable to implement roles based authorization to work on my controllers. SimpleMembership is turning out to be anything but simple.

我已搜查计算器,用Google搜索,并已读过<一个最新href=\"http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx\">John加洛韦的,试了很多建议,还没有能够resovle这个问题。这一切都始于获取SQL连接错误,无法弄清楚,为什么当连接字符串和一切良好。花了很多时间来弄清楚,导致问题是角色类。

I have searched stackoverflow, googled and have read the latest by John Galloway, tried many suggestions and still have not been able to resovle this issue. It all started with getting Sql connection error and could not figure out why when the connection string and everything else was good. It took many hours to figure out the it is Roles class that is causing problem.

在控制器中的[授权]属性和以前一样适用于基本验证。但任何时候,我尝试用角色它给SQL连接错误(因为它恢复到旧DefaultRolesProvider它试图连接到默认SqlEx preSS ASPNETDB文件和失败)。因此,像:

The [Authorize] attribute on controllers works as before for basic authentication. But any time I try to use Roles it give sql connection error (because it reverts to the old DefaultRolesProvider which tries to connect to default SqlExpress aspnetdb file and fails). So something like:

[Authorize(Roles="admin")]

不起作用。它将工作,如果我回到老asp.net成员资格提供者,但后来我通过OAuth失去了简单的数据库表,令牌基地确认和恢复,更安全的密码散列,更重要的外部登录。

does not work. It will work if I go back to the old asp.net membership providers, but then I lose the simple database tables, token bases confirmation and recovery, more secure password hashing and more importantly external logins via OAuth.

这里面code和剃刀意见工作的唯一的事情就是

The only thing that works inside code and razor views is

User.IsInRole("admin")

这是菜单项和确定这样的,但繁琐的版本控制器中的每一个行动内实施(我不喜欢,这一次只测试一个角色)。

which is OK for menu items and such, but ver cumbersome to implement inside every single Action in controller (and I do not like that it only tests for single role at a time).

我将大大AP preciate任何指导resovle这个问题。

I will greatly appreciate any guidance to resovle this issue.

推荐答案

找到答案这里由迈赫迪Golchin 的,这似乎照顾:

Found an answer here by Mehdi Golchin which seems to take care of:

[Authorize(Roles="admin,editor,publisher")]

如果我也是这个添加到主控制器:

If I also add this to the home controller:

 [InitializeSimpleMembership]

由于这个属性是账户控制器上,SimpleMembership数据库中获取初始化后,才第一次使用该账户控制器一样登录/注册。即使在当前用户被从cookie登录,数据库没有初始化,所以它抛出一个错误。一个解决办法是把这个属性的家居控制器,当我启动我的网站,被调用的。但是,则需要将其置于每个控制器上,因为我检查角色和显示基于角色不同的菜单项。

Because this attribute is on the Accounts controller, SimpleMembership database gets initialize only after the first use of the accounts controller like login/register. Even when the current user gets logged in from the cookie, the database is not initialized and so it throws an error. One solution is to put this attribute on the home controller which gets called when I launch my Website. But, then it needs to be placed on every controller because I check roles and display different menu items based on role.

这是设计不良的数据库应在App_Start被初始化,而不是第一次使用的时候。

This is poor design as the database should be initialized on the App_Start and not when first used.

我曾尝试把

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

的Global.asax 的Application_Start(),它需要的角色照顾使用菜单项检查 User.IsInRole(管理员),但随后抛出的错误在任何控制器与 [授权(角色=管理员)] 属性,即使有额外的属性 [InitializeSimpleMembership] 被应用。

in the Global.asax Application_Start(), and it takes care of role checking in menu items using User.IsInRole("admin"), but then throw error in any controller with [Authorize(Roles="admin")] attribute, even with additional attribute [InitializeSimpleMembership] is applied.

所以现在的解决方案是将`[InitializeSimpleMembership]上的所有控制器,用户可以在任何网页上使用外部链接最初降落。

So right now the solution is to put `[InitializeSimpleMembership] on all controllers as a user may initially land on any page using external links.

它仍然想不通如何初始化SimpleRolesProvider类做多角色管理,而不是仅仅 User.IsInRole()

It still can't figure how to initialize the SimpleRolesProvider class to do more of the role management instead of just User.IsInRole().

这些东西在WebMatrix的网页网站更好地工作,显然端口OT MVC是不完整的。它的冲突和获取与默认的asp.net成员资格提供混淆。

These things do work better in the webmatrix webpages site and obviously the port ot MVC is not complete. It conflicts and gets confused with the default asp.net membership providers.

修改
OK我并没有考虑 [InitializeSimpleMembership] 过滤器可以在全球范围放上这行中的 FilterConfig.cs 中的应用App_Start文件夹:

EDIT OK I was not thinking [InitializeSimpleMembership] filter could be applied globally by putting this line in the FilterConfig.cs in the App_Start folder:

filters.Add(new InitializeSimpleMembershipAttribute());

这需要这个问题的照顾。现在需要SimpleRolesProvider初始化一个解决方案,否则我将不得不写我自己的角色提供商。

That takes care of that problem. Now need a solution for SimpleRolesProvider initialization or else I will have to write my own roles provider.

这<一个href=\"http://odeto$c$c.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx\">post由斯科特·艾伦已经解决了我的所有问题。

This post by Scott Allen has solved all my problems.

通过包含该web.config中:

By including this in web.config:

<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>

角色和会员类的所有方法变得可用,并且可以在code初始化如下:

all the methods of Roles and Membership classes become available and can be initialized in code as follows:

var roles = (SimpleRoleProvider) Roles.Provider;
var membership = (SimpleMembershipProvider) Membership.Provider;

这篇关于在新的MVC 4互联网使用模板simplemembership基于角色的身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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