簡體   English   中英

Windows身份驗證用戶授權

[英]Windows Authentication User Authorization

我正在開發一個使用ASP.NET MVC應用程序的項目,該應用程序上只有一個用戶可以訪問的頁面。 使用Windows身份驗證,我要獲取User.Identity.Name並對照數據庫中Users表中的LogonID字段進行檢查。 如果存在匹配項,那么我想檢查IsAdmin字段是否等於true,如果是,則授予對所需頁面的訪問權限。

我對此很陌生,所以我想知道我將如何去做?

更新:

因此,我嘗試使用已向我建議的AuthorizeAttribute,但遇到了一個問題。

我正在使用沒有DBContext的SQL Server Compact數據庫。 所以我想知道如何編寫我的實體以便訪問數據庫?

public class AuthorizeAuthorAttribute : AuthorizeAttribute
{
    //Entity to access Database
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized)
        {
            return false;
        }
        string currentUser = httpContext.User.Identity.Name; 

        var userName = //Linq statement 

        string my = userName.ToString();

        if (currentUser.Contains(my))                       
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.Result = new HttpUnauthorizedResult();
    }
}

您可以實現定制的Authorize過濾器,並使用該過濾器裝飾所需的控制器。 在自定義過濾器Authorize核心方法中,對照您的數據庫檢查Windows身份,並相應地返回true / false。

https://msdn.microsoft.com/zh-CN/library/system.web.mvc.authorizeattribute.authorizecore(v=vs.118).aspx

暫無
暫無

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

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