Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于Windows身份验证的ASP.NET 5自定义角色_Asp.net_Asp.net Mvc_Authentication_Windows Authentication_Asp.net Core - Fatal编程技术网

用于Windows身份验证的ASP.NET 5自定义角色

用于Windows身份验证的ASP.NET 5自定义角色,asp.net,asp.net-mvc,authentication,windows-authentication,asp.net-core,Asp.net,Asp.net Mvc,Authentication,Windows Authentication,Asp.net Core,我正在使用ASP.NET 5构建一个intranet类型的站点,该站点使用Windows身份验证。我的身份验证工作正常,但我不希望域中的每个人都能访问intranet站点。我无法使用域角色,因此我已在SQL Server中设置了自己的自定义角色。我有一个将域用户名映射到角色的表。我想限制只有在我的SQL Server角色表中定义了角色的用户才能访问intranet站点。如何在ASP.NET 5中为Windows身份验证设置自定义角色?谢谢 您不需要设置自定义角色。您需要创建自定义授权属性,如下所

我正在使用ASP.NET 5构建一个intranet类型的站点,该站点使用Windows身份验证。我的身份验证工作正常,但我不希望域中的每个人都能访问intranet站点。我无法使用域角色,因此我已在SQL Server中设置了自己的自定义角色。我有一个将域用户名映射到角色的表。我想限制只有在我的SQL Server角色表中定义了角色的用户才能访问intranet站点。如何在ASP.NET 5中为Windows身份验证设置自定义角色?谢谢

您不需要设置自定义角色。您需要创建自定义授权属性,如下所述

更新:

是的,您可以全局使用自定义授权属性。假设这是您的自定义授权属性:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var username = httpContext.User.Identity.Name;

        // Check to see if user has a role in the database
        var isAuthorized = db.User.Find(username).Any();

        return isAuthorized;
    }
}
然后,您可以在操作级别或控制器级别使用它,如下所示:

[MyAuthorize]
public ActionResult Index()
{
}
public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new MyAuthorizeAttribute());
    }
}
或者,您可以在App_Start文件夹下的FilterConfig类中将其注册为全局筛选器,如下所示:

[MyAuthorize]
public ActionResult Index()
{
}
public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new MyAuthorizeAttribute());
    }
}

你看过SqlRoleProvider吗?我不相信SqlRoleProvider在ASP.NET 5中可用。至少我找不到它的任何引用。自定义授权属性是否可以在每个请求上自动运行?我认为这些只是在特定控制器上添加一个属性,比如[MyCustomAttribute],以限制特定控制器上的用户。