簡體   English   中英

MVC應用程序中的授權和身份驗證

[英]Authorization and authentication in MVC application

MVC應用程序中的授權和身份驗證

我有一個使用MVC 2在C#中開發的內部Web應用程序。我想使用AD角色/組來進行授權。 因此我有3個訪問組Admin,Basic,Readonly。 將通過這些組控制對應用程序的訪問。

現在,當我點擊我的MVC應用程序的操作/頁面時,要求是:

1)檢查訪問級別(在Admin,Basic或Readonly組中)

2)如果在一個組中 - 為頁面提供服務。 如果不是 - 請提供401 Unauthorized頁面。

我可能會對概念授權/身份驗證感到困惑,但到目前為止它是如何設置的(從答案,谷歌和我自己的努力來自這個問題

public static class AuthorizationModule
    {
        public static bool Authorize(HttpContext httpContext, string roles)
        {
            ...
            //Check Configuration.AppSettings for the roles to check

            //using httpContext.User check .IsInRole for each role and return true if they are

            ...

            //other wise throw new HttpException(401,.....)
        }

        ...
    }

    public class AuthorizeByConfigurationAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            //Essentially at the moment this is pretty much the same as AuthorizationModule.Authorize(HttpContext httpContext, string roles)
        }

    }

    //This code from http://paulallen.com.jm/blog/aspnet-mvc-redirect-unauthorized-access-page-401-page
    public class RequiresAuthenticationAttribute : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                filterContext.Result = new ViewResult {ViewName = "AccessDenied"};
            }
            else
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
        }
    }

這個問題是我現在需要兩次裝飾我的動作方法,ala:

[AuthorizeByConfiguration(Roles = "Admin, Basic, Readonly")]
        [RequiresAuthentication(Roles = "Admin, Basic, Readonly")]
        public ActionResult Index(string msg)
        {
            ...
        }

接下來的問題是,似乎我有三種不同的方法都試圖做同樣的事情。 我根據建議重寫方法,並不完全確定它們最初是如何工作的。 我怎么能實現我的要求?

編輯:由於這是一個IntrAnet應用程序,所有使用其網絡帳戶登錄的用戶都可以訪問此應用程序。 我需要限制訪問權限,以便只有屬於某些Active Directory安全組的人才能訪問此應用程序

我已經使用接口IAuthorization包裝了有關auth的所有方法。

下面是一個示例自定義attrbiute,您需要添加Roles屬性和您自己的實現。

出於可測試性原因,屬性調用過濾器本身。

public class SomeAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        var filter = new SomeAuthorizeFilter(DependencyLookup.Resolve<IAuthorization>());
        filter.OnAuthorization(filterContext);
    }
}

public class SomeAuthorizeFilter : IAuthorizationFilter
{
    private readonly IAuthorization _authorization;

    public SomeAuthorizeFilter(IAuthorization authorization)
    {
        _authorization = authorization;
    }

    protected virtual ActionResult ResultWhenNotAuthenticated(AuthorizationContext filterContext)
    {
        //snip..

        //default
        RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary
                                                            {
                                                                {"action", "Index"},
                                                                {"controller", "Home"}
                                                            };
        return new RedirectToRouteResult(redirectTargetDictionary);
    }

    #region IAuthorizationFilter Members

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if (!_authorization.GetCurrentUserIdentity().IsAuthenticated)
        {
            filterContext.Result = ResultWhenNotAuthenticated(filterContext);
        }
    }

    #endregion
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM