MVC4 and EF5 with Active Directory Authentication and Roles in SQL

I need to implement a project in MVC4 and EF5 using Active Directory authentication, but instead of having AD groups for roles, I have to implement Roles in SQL.

So far I have received the support of some individuals ... I hope this helps.

  • Role
  • User
  • UserRole

I am trying to use the [Role] annotation. Is it possible?

I lost a little, I need help PLEASE !!!!

0
source share
1 answer

I think I did it.

Please feel free to improve and reconfigure 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;

    }

}
0
source

All Articles