MVC4和EF5在SQL Active Directory验证和角色
问题描述:
我需要实现MVC4一个项目,并与EF5 Active Directory验证但不是具有角色的广告组,我必须SQL实现角色。
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.
- 角色
- 用户
- 的UserRole
我试图用[作用]注释。这可能吗?
I'm trying to use [Role] annotation. Is it possible?
我有点失落,我需要帮助,请!!!!
I'm a little lost, I need a Help PLEASE!!!!
答
我想我明白了。
请随时改进和重新发布它:
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;
}
}