I'm trying to create an MVC5 application that uses Windows Authentication but uses roles pulled from a User model.
I've searched high and low for an example, but the only ones I can find are based on the old ASP.NET identity framework.
Anyone care to point me in the right direction?!
Thanks!
So I've figured out one approach to solving this problem. I created a custom Authorization Attribute that checks the User model for a 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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With