0

I need to implement a project in MVC4 and EF5 with Active Directory Authentication but instead of having AD Groups for Roles, I must implement Roles in SQL.

So far I got some entities to support...I hope it could help.

  • Role
  • User
  • UserRole

I'm trying to use [Role] annotation. Is it possible?

I'm a little lost, I need a Help PLEASE!!!!

sKhan
  • 9,694
  • 16
  • 55
  • 53
user2988717
  • 127
  • 3
  • 8

1 Answers1

0

I Think I Got it.

Please feel free to improve and repost it:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ARQAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool allowToUse = false;
        IPrincipal user = httpContext.User;
        if (!user.Identity.IsAuthenticated)
        {
            return false;
        }
        else{
        try
        {
           Arq.Core.DAL.ArqContext c = new ArqContext();
           if (c.Users.Where(u => u.UserName.ToUpper() == user.Identity.Name.ToUpper()).FirstOrDefault().role.RoleDescription == "ADMINS")
           {
               allowToUse = true;
           }
        }
        catch (Exception)
        {

            allowToUse = false;
        }
        }          
            return allowToUse;

    }

}
user2988717
  • 127
  • 3
  • 8