2015-02-06 1 views
2

Я создаю интернет-приложение ASP.NET MVC 4.Аутентификация на основе ролей с идентификатором aspnet в ASP.NET MVC 4

В этом приложении я создал страницу входа, которую любой пользователь может войти в систему, а затем разрешил перенаправлять пользователя на разные страницы в зависимости от их роли.

ASP.NET Identity - это система членства здесь.

Это мой метод Войти Контроллер:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     var user = await UserManager.FindAsync(model.UserName, model.Password); 

     if (user != null) 
     { 
      if (user.ConfirmedEmail == true) 
      { 

       await SignInAsync(user, model.RememberMe); 

       if (String.IsNullOrEmpty(returnUrl)) 
       { 
        if (UserManager.IsInRole(user.Id, "HEC_Admin")) 
        { 
         return RedirectToAction("Index", "HEC"); 
        } 
        //role Admin go to Admin page 
        if (UserManager.IsInRole(user.Id, "HEI_User")) 
        { 
         return RedirectToAction("Index", "HEI"); 
        } 
       } 

       else 
       { 
        return RedirectToLocal(returnUrl); 
       } 


      } 
      else 
      { 
       ModelState.AddModelError("", "Confirm Email Address."); 
      } 
     } 
     else 
     { 
      ModelState.AddModelError("", "Invalid username or password."); 
     } 
    } 
    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

Это ХЕИ контроллер Класс:

public class HEIController : Controller 
{ 
    // 
    // GET: /HEI/ 

    [Authorize(Roles = "HEI_User")] 
    public ActionResult Index() 
    { 
     return View(); 
    } 



} 

Это мой HEC контроллер Класс:

 public class HECController : Controller 

    { 
     // 
     // GET: /HEC/ 
     [Authorize(Roles = "HEC_Admin")] 
     public ActionResult Index() 
     { 
      return View(); 
     } 
    } 

, когда я удаляю [ Авторизовать (Роли = "HEC_Admin")] над действием индекса в HECC ontroller класс и когда я удалить [Авторизовать (Roles = «HEC_User»)] выше действия индекса в классе HEIController это работает отлично,

но как ограничить несанкционированный доступ к этим страницам?

ответ

2

У меня была та же проблема, что и вы и я до сих пор не знаем, почему это происходит. Я сделал это, чтобы создать свой собственный атрибут авторизации и сам проверить роль.

public class CustomAuthorizationAttribute : AuthorizeAttribute 
{ 
    public string IdentityRoles 
    { 
     get { return _identityRoles ?? String.Empty; } 
     set 
     { 
      _identityRoles = value; 
      _identityRolesSplit = SplitString(value); 
     } 
    } 

    private string _identityRoles; 
    private string[] _identityRolesSplit = new string[0]; 

    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     //do the base class AuthorizeCore first 
     var isAuthorized = base.AuthorizeCore(httpContext); 
     if (!isAuthorized) 
     { 
      return false; 
     } 
     if (_identityRolesSplit.Length > 0) 
     { 
      //get the UserManager 
      using(var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))) 
      { 
       var id = HttpContext.Current.User.Identity.GetUserId(); 
       //get the Roles for this user 
       var roles = um.GetRoles(id); 
       //if the at least one of the Roles of the User is in the IdentityRoles list return true 
       if (_identityRolesSplit.Any(roles.Contains)) 
       { 
        return true; 
       } 
      } 
      return false; 
     } 
     else 
     { 
      return true; 
     } 

    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     //if the user is not logged in use the deafult HandleUnauthorizedRequest and redirect to the login page 
     if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
     { 
      base.HandleUnauthorizedRequest(filterContext); 
     } 
     else 
     //if the user is logged in but is trying to access a page he/she doesn't have the right for show the access denied page 
     { 
      filterContext.Result = new RedirectResult("/AccessDenied"); 
     } 
    } 

    protected static string[] SplitString(string original) 
    { 
     if (String.IsNullOrEmpty(original)) 
     { 
      return new string[0]; 
     } 

     var split = from piece in original.Split(',') 
        let trimmed = piece.Trim() 
        where !String.IsNullOrEmpty(trimmed) 
        select trimmed; 
     return split.ToArray(); 
    } 
} 

Я также добавил метод HandleUnauthorizedRequest для перенаправления на присвоенного странице, если пользователь вошел в систему, но не имеет доступа к этому действию или контроллер

Чтобы использовать это просто сделать это:

[CustomAuthorization(IdentityRoles = "HEI_User")] 
public ActionResult Index() 
{ 
    return View(); 
} 

Надеюсь, что это поможет.

+0

Спасибо, много работы, есть какая-либо комбинация между вашим подходом и этим http://stackoverflow.com/questions/31001039/forgot-password-method-edit-user-method-not-working – kez

Смежные вопросы