2014-04-14 2 views
0

제가 만든 간단한 역할 공급자가 있습니다.
LocalBankRoleProvider :사용자 지정 ASP.NET MVC 역할 공급자가 작동하지 않습니다.

public override bool IsUserInRole(string username, string roleName) 
    { 
     var user = _repository.GetUser(username); 
     var role = _repository.GetRole(roleName); 

     if (!_repository.UserExists(user)) 
      return false; 
     if (!_repository.RoleExists(role)) 
      return false; 
     return user.Role.Name == role.Name; 
    } 


    public override string[] GetRolesForUser(string username) 
    { 
     var role = _repository.GetRoleForUser(username); 
     if (!_repository.RoleExists(role)) 
      return new string[] { string.Empty }; 
     return new string[] { role.Name }; 
    } 

LocalBankMembershipProvider :

public override bool ValidateUser(string username, string password) 
    { 
     if (string.IsNullOrEmpty(password.Trim()) 
      || string.IsNullOrEmpty(username.Trim())) 
      return false; 
     var hash = LocalBankRepository.GetMd5Hash(password); 
     return _repository.GetAllUsers().Any(user => (user.Name == username.Trim()) 
      && (user.Password == hash)); 

    } 

HomeController :

[Authorize] 
    public string Public() 
    { 
     return "public"; 
    } 

    [Authorize(Roles = "Guests")] 
    public string Users() 
    { 
     return "users"; 
    } 

    [Authorize(Roles = "Administrators")] 
    public string Admin() 
    { 
     return "Admin"; 
    } 

AccountController :

[HttpGet] 
    public ActionResult LogOn(string returnUrl) 
    { 
     return View(); 
    } 


    [HttpPost] 
    public ActionResult LogOn(LogOnModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (_provider.ValidateUser(model.UserName, model.Password)) 
      { 
       if (_roleProvider.IsUserInRole(model.UserName, "Administrators")) 
       { 
        if (!string.IsNullOrEmpty(returnUrl)) return Redirect(returnUrl); 
        //return RedirectToAction("Index", "Home"); 
       } 
      } 
      ModelState.AddModelError("Password", "The user name or password provided is incorrect."); 
     } 
     return View(model); 
    } 

Global.asax에 :

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
    } 

그리고 WebConfig :

<membership defaultProvider="LocalBankMembershipProvider"> 
    <providers> 
    <clear /> 
    <add name="LocalBankMembershipProvider" type="WebApplication1.Abstract.LocalBankMembershipProvider" connectionStringName="UsersDbEntities" /> 
    </providers> 
</membership> 

<roleManager defaultProvider="LocalBankRoleProvider" enabled="true" cacheRolesInCookie="false"> 
    <providers> 
    <clear /> 
    <add name="LocalBankRoleProvider" type="WebApplication1.Abstract.LocalBankRoleProvider" connectionStringName="UsersDbEntities" /> 
    </providers> 
</roleManager> 

위의 기능이 작업의 모든

.

질문은 여전히 ​​약자
는 내가 관리 방법에 사용자를 리디렉션하려고하지만 작업 나던 및
어떤 아이디어를 LoginForm로 리디렉션?

+0

이 권한 부여 필터 코드를 게시? –

+0

그래, 내 문제의 해결책을 찾았 어. – stackoverflowusername

+0

도 게시하십시오. –

답변

0

좋아, 내 문제의 해결책을 찾았습니다.
문제는 다음과 같습니다

public static string GetMd5Hash(string value) 
    { 
     var md5Hasher = MD5.Create(); 
     var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value)); 
     var sBuilder = new StringBuilder(); 
     for (var i = 0; i < data.Length; i++) 
     { 
      sBuilder.Append(i.ToString("x2")); 
     } 
     return sBuilder.ToString(); 
    } 

은 다음과 같아야합니다

public static string GetMd5Hash(string input) 
    { 
     if (String.IsNullOrWhiteSpace(input)) 
      return String.Empty; 

     // step 1, calculate MD5 hash from input 
     MD5 md5 = System.Security.Cryptography.MD5.Create(); 
     byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); 
     byte[] hash = md5.ComputeHash(inputBytes); 

     // step 2, convert byte array to hex string 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < hash.Length; i++) 
     { 
      sb.Append(hash[i].ToString("x2")); 
     } 
     return sb.ToString(); 
    } 
관련 문제