2013-02-23 100 views
0

我很困惑如何在我的asp.net MVC4剃须刀项目中使用角色。 这两者之间有什么区别,主要是,我如何使用authorize属性,并使其在我检查经过身份验证的用户的角色时进入我的自定义角色提供程序。还是我在这里混合了一些东西?角色提供者和System.Web.Security.Roles

更具体的:

我有一个管理控制器,其中的角色为“管理员”用户可以做CRUD的东西。 在我控制我应用以下属性:

[Authorize(Roles = "administrator")] 
public class OverviewController : Controller 

它是正确的假设,该授权属性将用我的custome角色提供程序在后台?如果是这样,为什么它不适合我?我的自定义角色提供一流的

配件:

public sealed class CustomRoleProvider : RoleProvider 
{ 
    public override void Initialize(string name, NameValueCollection config) 
    { 
     if (config == null) throw new ArgumentNullException("config"); 

     if (name.Length == 0) name = "CustomRoleProvider"; 

     if (String.IsNullOrEmpty(config["description"])) 
     { 
      config.Remove("description"); 
      config.Add("description", "Custom Role Provider"); 
     } 

     //Initialize the abstract base class. 
     base.Initialize(name, config); 

     _applicationName = Helpers.GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath); 
    } 

    public override bool IsUserInRole(string email, string roleName) 
    { 
     bool isValid = false; 

     var usersInRole = _unitOfWork.UsersRepository.Get(uir => uir.email == email && uir.Roles.Name == roleName); 

     if (usersInRole != null) isValid = true; 

     return isValid; 
    } 

我在做什么不正确?如何用户,当他或她是否正确授权,像这样:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult LoginValidate(Authentication authentication, string returnUrl) 
    { 
     string email = authentication.email; 
     string password = authentication.password; 
     bool rememberMe = authentication.rememberMe; 
     if(string.IsNullOrEmpty(returnUrl)) returnUrl = "/"; 

     //If the filled in fields are validated against the attributes 
     if (ModelState.IsValid) 
     { 
      if (MembershipService.ValidateUser(email, password)) 
      { 
       FormsService.SignIn(email, rememberMe); 

       return RedirectToAction("Index", "Home", new { area="" }); 
      } 

      ModelState.AddModelError("", Resources.Resources.Error_incorrect_emailPassword); 

     } 

     // Add the ModelState dictionary to TempData here. 
     TempData["ModelState"] = ModelState; 

     return RedirectToAction("index", "Home", new { area="" }); 
    } 

从我的自定义角色提供对他或她的授权检查?

编辑

我的web.config:

<roleManager enabled="true" defaultProvider="CustomRoleProvider" cacheRolesInCookie="true" > 
    <providers> 
    <clear /> 
    <add name="CustomRoleProvider" type="ArtWebShop.Common.CustomRoleProvider" connectionStringName="ArtWebshopEntities" applicationName="/" /> 
    </providers> 
</roleManager> 

    <membership defaultProvider="CustomMembershipProvider"> 
    <providers> 
    <clear /> 
    <add name="CustomMembershipProvider" type="ArtWebShop.Common.CustomMembershipProvider" connectionStringName="ArtWebshopEntities" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="0" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> 
    </providers> 
</membership> 

编辑II

public override bool ValidateUser(string email, string password) 
    { 
     string salt = _unitOfWork.UsersRepository.GetSalt(email); 
     string hashedPassword = Helpers.CreatePasswordHash((password), salt); 

     return _unitOfWork.UsersRepository.UserIsValid(email, hashedPassword); 

    } 

回答

0

它是正确的假设,该授权属性会用我的 custome角色提供者在后端?

是的。

如果是这样,为什么它不适合我?

你可能忘了在你的web.config注册这个自定义角色提供商,使其成为该应用程序的默认提供:

<roleManager defaultProvider="CustomRoleProvider" enabled="true"> 
    <providers> 
     <clear /> 
     <add 
      name="CustomRoleProvider" 
      type="Somenamespace.CustomRoleProvider" 
     /> 
    </providers> 
</roleManager> 
+0

啊,NOP,忘了提,web.config文件应正确。我有你提供的相同结构。请参阅编辑。 – 2013-02-23 18:17:37

+0

然后这应该工作。还要确保你已经重写了'GetRolesForUser'方法。 – 2013-02-23 18:17:59

+0

因此,如果我在方法IsUserInRole的自定义角色提供程序中放置断点,并且将我的概述类顶部的授权属性放置在它应该停止在那里?每次需要将编辑II中的代码放在控制器的顶部吗? – 2013-02-23 18:21:23