Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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
C# Web api windows身份验证自定义角色提供程序不工作?_C#_Asp.net_Authentication_Asp.net Web Api_Asp.net Identity - Fatal编程技术网

C# Web api windows身份验证自定义角色提供程序不工作?

C# Web api windows身份验证自定义角色提供程序不工作?,c#,asp.net,authentication,asp.net-web-api,asp.net-identity,C#,Asp.net,Authentication,Asp.net Web Api,Asp.net Identity,我使用Visual studio 2015创建了一个新的Web API项目,运行良好。然后我添加了以下步骤 为角色、角色成员创建表,并首先使用代码创建所有数据访问代码 创建继承RoleProvider的自定义角色提供程序类 更新web.config文件以使用自定义角色提供程序 用[AuthorizeRoles=TestRole]装饰控制方法 详情: 将创建用于存储角色和成员资格的下表 create table Roles (Id int primary key, Role nvarchar(50

我使用Visual studio 2015创建了一个新的Web API项目,运行良好。然后我添加了以下步骤

为角色、角色成员创建表,并首先使用代码创建所有数据访问代码

创建继承RoleProvider的自定义角色提供程序类

更新web.config文件以使用自定义角色提供程序

用[AuthorizeRoles=TestRole]装饰控制方法

详情:

将创建用于存储角色和成员资格的下表

create table Roles (Id int primary key, Role nvarchar(50) not null)
create TABLE RoleMember (RoleId int references Roles(Id), userId varchar(50) not null primary key(RoleId, userId))
自定义角色提供程序代码为

namespace webapi.Models
{
    public class MyRoleProvider : RoleProvider
    {
        public override string ApplicationName { .... }
        public override void AddUsersToRoles(string[] usernames, string[] roleNames) { .... }
        public override void CreateRole(string roleName) { .... }
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { .... }
        public override string[] FindUsersInRole(string roleName, string usernameToMatch) { .... }
        public override string[] GetAllRoles() { .... }
        public override string[] GetRolesForUser(string username)
        {
            using (var db = new MyContext()) // break point set here
            {
                var roles = from rm in db.RoleMembers
                            from r in db.Roles
                            where r.Id == rm.RoleId && rm.userId == username
                            select r.Role;
                if (roles!=null)
                {
                    return roles.ToArray();
                }
                else
                {
                    return new string[] { };
                }
            }
        }

        public override string[] GetUsersInRole(string roleName) { .... }
        public override bool IsUserInRole(string username, string roleName)
        {
            using (var db = new MyContext()) // Break point set here
            {
                var roles = from rm in db.RoleMembers
                            from r in db.Roles
                            where r.Id == rm.RoleId && rm.userId == username
                            select r.Role;
                if (roles!=null)
                {
                    return roles.Any(r=> r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                }
                else
                {
                    return false;
                }
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { .... }
        public override bool RoleExists(string roleName) { .... }
    }
}
web.config文件已更新

<system.web>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
    <roleManager cacheRolesInCookie="true" defaultProvider="MyRoleProvider" enabled="true">
      <providers>
        <clear/>
        <add name="MyRoleProvider" type="webapi.Models.MyRoleProvider"/>
      </providers>
    </roleManager>
</system.web>
然而,测试得到的错误是 此请求的授权已被拒绝。
因此,我在自定义角色提供程序中设置了断点。但在调试时它们都不能被击中

您正在使用OWIN Auth吗?您确定在通话中传递了正确的标题吗?因为如果没有授权用户,调用将不会到达断点。我可能错了,但我想它会先检查是否有有效的令牌,然后验证角色。我使用的是Windows身份验证。该项目是使用VS 2015中的模板创建的,所以我猜是OWIN?是的,我想是。您是否在http调用的头中发送令牌?我不知道如何在http调用中发送令牌?我在web.config中更改了,所以我认为IIS将处理这些细节?现在我还没有客户。我正在通过在浏览器中键入url进行测试。你说得对!您不必在http请求中传递任何凭据。请原谅我。
namespace webapi.Controllers
{
    [Authorize(Roles = "TestRole")] // The web api call returns value before add this line
    public class MyController : ApiController
    {