C# MVC4表单身份验证Active Directory自定义授权属性
在我的C#MVC4应用程序中,我使用基于表单的身份验证和Active Directory。我有一个自定义的广告会员提供商。我已经成功地测试了它可以读取和验证用户所属的组。现在,我正在尝试创建一个自定义授权属性,该属性将执行以下操作:C# MVC4表单身份验证Active Directory自定义授权属性,c#,asp.net-mvc-4,active-directory,authorization,forms-authentication,C#,Asp.net Mvc 4,Active Directory,Authorization,Forms Authentication,在我的C#MVC4应用程序中,我使用基于表单的身份验证和Active Directory。我有一个自定义的广告会员提供商。我已经成功地测试了它可以读取和验证用户所属的组。现在,我正在尝试创建一个自定义授权属性,该属性将执行以下操作: if (user is logged-in/not timed-out/authenticated) { if (user's role is equal to role 1 or role 2) { return a specif
if (user is logged-in/not timed-out/authenticated)
{
if (user's role is equal to role 1 or role 2)
{
return a specific view or (preferably) perform a specific redirect to action
}
else
{
return a different specific view or (preferably) perform a different specific redirect to action
}
}
else
{
return View
}
以下是我到目前为止的情况:
public class AuthorizeEditAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (httpContext.Request.IsAuthenticated)
{
if ((httpContext.User.IsInRole("group1")) || (httpContext.User.IsInRole("group2")))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
我不知道如何执行重定向任务。我已经看了这篇文章,讨论了如何进行重定向,但不明白如何将其与目前为止的文章结合起来。特别是因为我认为我必须使用AuthorizationCore来获得对httpcontext.user的访问权,这是我执行的第一次检查,我不知道如何传入AuthorizationContext类型的另一个参数,该参数似乎是沿着重定向的所需路径传递的。我认为您也应该覆盖该方法。这有一个
AuthorizationContext
参数,允许您将结果设置为您喜欢的RedirectResult
。请查看是否有帮助