Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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# .NET Core 2.1 Windows身份验证无法识别角色成员身份_C#_.net_Iis Express_Core - Fatal编程技术网

C# .NET Core 2.1 Windows身份验证无法识别角色成员身份

C# .NET Core 2.1 Windows身份验证无法识别角色成员身份,c#,.net,iis-express,core,C#,.net,Iis Express,Core,我已经完成了教程和StackOverflow问题的学习,但仍然存在一个问题,即在使用[Authorize]属性修饰的方法上得到403禁止 这是一种有效的方法,证明Google Chrome在调试期间正在将我的Windows凭据传递到IISExpress中运行的站点 public IActionResult Index() { ViewBag.UserIdentityIsAuthenticated = User.Identity.IsAuthenticate

我已经完成了教程和StackOverflow问题的学习,但仍然存在一个问题,即在使用[Authorize]属性修饰的方法上得到403禁止

这是一种有效的方法,证明Google Chrome在调试期间正在将我的Windows凭据传递到IISExpress中运行的站点

public IActionResult Index()
        {
            ViewBag.UserIdentityIsAuthenticated = User.Identity.IsAuthenticated;
            ViewBag.UserIdentityName = User.Identity.Name;
            ViewBag.UserIsInRoleTechnical = User.IsInRole("ADSecurityGroupOne");
            ViewBag.UserIsInRoleTechnicalPeople = User.IsInRole("ADSecurityGroupOneTwo");

            return View();
        }
这是一个失败的方法,403应该只显示视图,它还没有链接到任何数据库

    [Authorize(Policy = "AllowedOnly")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Index(AccountViewModel model)
    {
        if (ModelState.IsValid)
        {
            return View();
        }
        else
            return View();
    }
这是Startup.cs中的ConfigureServices方法

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddOptions();
            ApplicationSettings appSettings = new ApplicationSettings();
            Configuration.GetSection("ApplicationSettings").Bind(appSettings);

            services.AddAuthentication(IISDefaults.AuthenticationScheme);
            services.AddAuthorization(options => options.AddPolicy("AllowedOnly", policy => policy.RequireRole(appSettings.AllowedToMigrate)));
        }
我已经确认AllowedToMigrate的值与appsettings.json中指定的值相同

{
  "ApplicationSettings": {
    "AllowedToMigrate": "ADSecurityGroupOne,ADSecurityGroupTwo"
  },

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

为什么[AuthorizePolicy=AllowedOnly]失败?

我认为您需要将AllowedToMigrate值拆分为组件角色,而不仅仅是作为一个字符串提交

你真正想要达到的是

services.AddAuthorization(options => options.AddPolicy("AllowedOnly", 
    policy => policy.RequireRole("ADSecurityGroupOne", "ADSecurityGroupTwo")));
我不完全确定您将如何通过单个配置设置实现这一点,可能是通过创建一个新的需求:


回答我自己的问题,因为我知道我做错了什么:

如果有多个字符串,则角色必须作为字符串数组提供;如果只有一个字符串,则角色只能作为单个字符串提供。我更新了json应用程序设置文件和ApplicationSettings类,以便AllowedToMigrate是一个字符串数组

{
  "ApplicationSettings": {
    "AllowedToMigrate": [ "ADSecurityGroupOne", "ADSecurityGroupTwo" ]
  },

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

我还修复了角色名的输入错误,这是最初的问题!因此,这个故事的寓意是:绝对确保您使用的角色名称拼写正确,并且在授权似乎无缘无故出错时,始终尝试使用不同的角色。

我最初允许作为字符串数组进行迁移,但具有相同的行为。如果我手动运行.RequireRoleADSecurityGroupOne,它会工作,我现在不明白为什么。