在ASP MVC 5角色管理(Microsoft.AspNet.Identity) [英] Role Management in ASP MVC 5 (Microsoft.AspNet.Identity)

查看:145
本文介绍了在ASP MVC 5角色管理(Microsoft.AspNet.Identity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP MVC5 RC我没得到角色系统正常工作。
我的数据库有需要的所有表的角色存在,但如果打样用户在角色总是返回false(不SQL异常或东西)!?

in ASP MVC5 RC I didn't get the role system to work. My database has all needs tables an role exist but proofing if user is in role always return false (no SQL exception or something)!?

难道我需要激活角色系统的IPrincipal 地方?

Did I need to activate role system for IPrincipal somewhere?

测试code:

AccountController accCont = new AccountController();

// check role exist : result = true
var roleExist = await accCont.IdentityManager.Roles.RoleExistsAsync("61c84919-72e2-4114-9520-83a3e5f09de1");

// try find role by name : result = role object
var role = await accCont.IdentityManager.Roles.FindRoleByNameAsync("ProjectAdministrator");

// check with AccountController instance :  result = true
var exist = await accCont.IdentityManager.Roles.IsUserInRoleAsync(User.Identity.GetUserId(), role.Id);

// check if current user is in role : result (both) = false????
var inRole = User.IsInRole(role.Id);
var inRole2 = User.IsInRole(role.Name);

我也尝试建立像 Microsoft.AspNet.Identity.Owin IIdentity.GetUserId()扩展方法的定制减轻罪责C $ C>命名空间。

I also try to build an custom extenuation like the IIdentity.GetUserId() extension method from Microsoft.AspNet.Identity.Owin Namespace.

namespace Microsoft.AspNet.Identity
{
   public static class IdentityExtensions
   {
       public static string IsUserInRole(this IIdentity identity)
       {
           if (identity == null)
           {
               throw new ArgumentNullException("identity");
           }
           ClaimsIdentity identity2 = identity as ClaimsIdentity;
           if (identity2 != null)
           {
               var result = identity2.FindFirstValue(IdentityConfig.Settings.GetAuthenticationOptions().RoleClaimType);

               return null; // later result
           }
           return null;
       }
   }
}

但结果对索赔类型 RoleClaimType 总是 :(
我真的坚持这一点。

But the result for claim Type RoleClaimType is always null :( I'm really stuck with this.

感谢您的帮助!斯特芬

推荐答案

我想了解如何在MVC 5我自己,这就是把我带到这里使用的角色。我不能回答你的问题,但是看看这个链接。下载的解决方案作品的权利开箱,我已经能够剪切和粘贴一些code,并把它在我自己的应用程序的工作。现在,我想完全理解它在做什么。

I'm trying to understand how to use roles in MVC 5 myself, which is what brought me here. I can't answer your question, but check out this link. The downloaded solution works right out of the box and I've already been able to cut-and-paste some of the code and get it working in my own app. Now I'm trying to fully understand what it's doing.

<一个href=\"http://www.typecastexception.com/post/2013/11/11/Extending-Identity-Accounts-and-Implementing-Role-Based-Authentication-in-ASPNET-MVC-5.aspx\" rel=\"nofollow\">http://www.typecastexception.com/post/2013/11/11/Extending-Identity-Accounts-and-Implementing-Role-Based-Authentication-in-ASPNET-MVC-5.aspx

这可能不是回答你的问题,但至少这是因为没有很多的麻烦描述的实际上做的工作完全可行的解决方案,所以这是一个很好的起点。

It may not answer your question but at least it's a fully working solution that actually does work as described without a lot of hassle, so it's a good starting point.

这篇关于在ASP MVC 5角色管理(Microsoft.AspNet.Identity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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