【问题标题】:Windows Authentication User AuthorizationWindows 身份验证用户授权
【发布时间】:2016-08-15 14:31:19
【问题描述】:

我正在开发一个使用 ASP.NET MVC 应用程序的项目,该应用程序上有一个只有特定用户才能访问的页面。使用 Windows 身份验证,我想获取 User.Identity.Name 并检查数据库中我的用户表中的 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();
    }
}

【问题讨论】:

  • 有一个自定义的授权过滤器可以满足您的需要。
  • @WiktorZychla 请查看更新并建议您是否可以提供进一步帮助

标签: asp.net asp.net-mvc authorization windows-authentication


【解决方案1】:

您可以实现自定义授权过滤器并使用此过滤器装饰所需的控制器。在自定义过滤器授权核心方法中,根据您的数据库检查 Windows 身份并相应地返回真/假。

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

【讨论】:

    猜你喜欢
    • 2017-05-28
    • 2013-10-02
    • 1970-01-01
    • 2017-05-05
    • 2012-11-09
    • 2018-07-09
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多