ASP.NET MVC 5 Identity 2.0, Windows Auth, a user model with a role attribute

I am trying to create an MVC5 application that uses Windows authentication, but uses roles pulled from the user model.

I searched high and low for an example, but the only ones I can find are based on the old ASP.NET authentication system.

Anyone want to point me in the right direction ?!

Thanks!

+5
source share
1 answer

So, I understood one approach to solving this problem. I created a custom authorization attribute that validates the User model for the role.

using System.Linq; using System.Web; using System.Web.Mvc; using App.Models; using System.Security.Claims; namespace App.Extensions.Attributes { public class AuthorizeUser : AuthorizeAttribute { Context context = new Context(); protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) return false; string login = ClaimsPrincipal.Current.Claims.ElementAt(1).Value.Split('@')[0]; string[] roles = base.Roles.Split(','); User user = context.Users.FirstOrDefault(u => u.Login == login); if (user == null) return false; else if (base.Roles == "") return true; else return roles.Contains(user.Role.ToString()); } } } 

Thoughts?

+1
source

Source: https://habr.com/ru/post/1203254/


All Articles