SimpleMembershipProvider角色无法访问 [英] SimpleMembershipProvider roles not accessible

查看:227
本文介绍了SimpleMembershipProvider角色无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SimpleMEmbershipProvider认证机制MVC4应用。

I have MVC4 application which uses SimpleMEmbershipProvider for authentication mechanism.

一切工作正常,除了当我返回到应用程序,并使用持续性cookie的验证。

Everything works fine, apart of when I return to the application and authenticate using persistant cookie.

我验证的罚款,但不能访问,我分配到的角色。实际上,在所有不能访问的角色:

I am authenticated fine, but cannot access roles that I am assigned to. Effectively, cannot access roles at all:

string.Join(",", Roles.GetRolesForUser(User.Identity.Name)) 

返回空字符串

什么可能导致的?

推荐答案

当SimpleMembershipProvider尚未初始化发生这种情况。这个例子MVC窗体身份验证模板假定你将让您的网站的匿名访问,直到你到登录页面不初始化成员资格提供程序。然而,更常见的安全技术要求的任何网站访问的用户名和定义在_layout页面菜单选项由角色来决定。但是,如果你使用的持久性cookie,你不这么验证用户的角色不是从会员数据库加载重温登录页面。

This can happen when the SimpleMembershipProvider hasn't been initialized. The example MVC forms authentication template assumes that you'll be allowing anonymous access to your site and doesn’t initialize the membership provider until you go to the login page. However, a more common security technique is to require a login for any site access and to define menu choices in the _layout page to be determined by roles. But, if you use the persistent cookie, you don’t revisit the login page so the roles for the authenticated user aren’t loaded from the membership database.

您想要做的是当用户输入使价值得到加载该网站初始化提供商。要做到这一点,要添加以下过滤器在App_Start文件夹中的一个FilterConfig类的RegisterGlobalFilters方法

What you want to do is initialize the provider when the user enters the site so that values get loaded. To do this, you want to add the following filter in the RegisterGlobalFilters method of the FilterConfig class in the App_Start folder

filters.Add(new YourAppNameSpace.Filters.InitializeSimpleMembershipAttribute());

此将导致从数据库加载的用户数据时的cookie验证的用户进入网站

This will cause the user data to be loaded from the database when a cookie authenticated user enters the site.

另一种替代技术是将[InitializeSimpleMembership]装饰添加到该cookie autheticated用户可能直接进入任何控制器的方法。这是一种,但如果你必须把它放在了不少控制器的凌乱。因此,把它在全球滤波器是在大多数情况下,较好

Another alternative technique is to add the [InitializeSimpleMembership] decorator to any controller method that cookie autheticated users might enter directly. This is kind of messy though if you have to put it on a lot of controllers. Therefore, putting it in the global filter is better in most cases.

这篇关于SimpleMembershipProvider角色无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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