Rider:ASP.NET Core 3.1中的MacOS开发:Windows身份验证

时间:2020-03-10 20:10:29

标签: c# asp.net-core .net-core asp.net-core-3.1

我最近从装有Visual Studio的Windows机器切换到装有Rider的MacBook。我有一个从.NET Framework 4.7.2升级的.NET Core 3.1项目,并且我正在尝试为其配置Windows身份验证。

我的MacBook通过Enterprise Connect连接到公司域,并且我有活动的kerboros票证。又名,我能够访问使用Windows身份验证或以其他方式提取我的域凭据的公司资源。例如,我可以登录到在IIS和.NET Framework 4.7.2上运行的项目的发布版本。

但是,我在本地运行.NET Core 3.1版本时无法使用Windows身份验证。我对项目进行了以下更改:

StartUp.cs中,将以下内容添加到ConfigureServices

services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
services.AddAuthorization(options =>
{
    options.AddPolicy(nameof(Policy.AuthorizedUser), 
        policy => policy.Requirements.Add(new AuthorizedUserRequirement()));
});

services.AddSingleton<IAuthorizationHandler, AuthorizedUserHandler>();

StartUp.cs中,将以下内容添加到Configure

app.UseAuthentication();
app.UseAuthorization();

我确实安装了Microsoft.AspNetCore.Authenticate.Negotiate软件包。

创建了授权处理程序和要求:

public class AuthorizedUserHandler: AuthorizationHandler<AuthorizedUserRequirement>
{
    private readonly AuthorizationConfiguration _configuration;

    public AuthorizedUserHandler(AuthorizationConfiguration configuration)
    {
        _configuration = configuration;
    }

    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AuthorizedUserRequirement requirement)
    {
        if (context.User.IsInRole(_configuration.User.Group))
        {
            // Call 'Succeed' to mark current requirement as passed
            context.Succeed(requirement);
        }

        return Task.CompletedTask;
    }
}
public class AuthorizedUserRequirement: IAuthorizationRequirement { }

然后,将其应用于我的控制器以限制访问权限:

[Authorize(Policy = nameof(Policy.AuthorizedUser))]
public class CustomerController : Controller
{
}

我的配置对象已正确注入。但是,context.User为空。当我调试项目并转到使用该策略的页面(在Google Chrome中)时,我从Google Chrome收到HTTP错误401默认错误页面。

如何修改我的代码以解决此错误,或者如何设置Windows身份验证以在MacOS上进行本地调试?我还尝试在services.AddAuthentication中使用Windows身份验证架构,但这也失败了。

0 个答案:

没有答案