繁体   English   中英

访问ASP.NET Core中间件中的用户凭据

[英]Access to user credentials in ASP.NET Core middleware

在ASP.NET核心(2.1)中,在Windows上运行,我使用配置了以下身份验证方案的HttpSys:

builder.UseHttpSys(options =>
        {
            options.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
            options.Authentication.AllowAnonymous = true;
        })

然后在我的Startup.Configure()方法中,我试图访问调用uri“/ sensitiveOperation”的客户端的用户凭据,如下所示:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseAuthentication();

        app.MapWhen(context => context.Request.Path.Equals("/sensitiveOperation") && context.Request.Method.Equals(HttpMethods.Put), subApp =>
        {                
            subApp.Run(async (context) =>
            {
                if (context.User.Identity.Name == "admin")
                {
                    await context.Response.WriteAsync("Performing sensitive operation.");
                    // .. Do Sensitive operation....
                }                    
            });
        });

这个例子略显粗俗,但主要的一点是context.User.Identity.Name总是空的,我希望看到正在进行调用的AD帐户的名称。 请注意,调用是在powershell中完成的,如下所示:

Invoke-WebRequest -Uri http://localhost:5555/sensitiveOperation -Method Put -UseDefaultCredentials

我可以将此代码放在控制器中并使用[Authorize]属性来获取凭据,但我更愿意在点击Mvc管道之前执行此操作。 有没有办法让用户处于管道的早期阶段?

更改AllowAnonymous

options.Authentication.AllowAnonymous = false;

如果您有匿名,并且您没有提示进行身份验证,则浏览器不会进行身份验证。 即使你发送创建asp.net也不会得到它们,除非在控制器/方法上有一个Authenticate属性,或者如果你要去函数路由,你会调用signin。

如果您不希望将AllowAnonymous设置为false ,则可以尝试使用context.ChallengeAsync基于Credential对请求进行身份验证。

这是代码:

        app.MapWhen(context => context.Request.Path.Equals("/sensitiveOperation") && context.Request.Method.Equals(HttpMethods.Put), subApp =>
        {
            subApp.Run(async (context) =>
            {
                var authService = context.RequestServices.GetRequiredService<IAuthorizationService>();

                if (!context.User.Identity.IsAuthenticated)
                {
                    //await context.ChallengeAsync("Windows"); //Option1
                    //await context.ChallengeAsync();  //Option2
                    await context.ChallengeAsync(HttpSysDefaults.AuthenticationScheme); //Option3
                }
                if (context.User.Identity.Name == "admin")
                {
                    await context.Response.WriteAsync("Performing sensitive operation.");
                    // .. Do Sensitive operation....
                }
            });
        });

注意 ,这样, subApp.Run将运行两次,第一个请求是UnAuthenticated,它将挑战凭证,第二个请求是Authenticated, context.User.Identity.Name将具有值。 这个过程是后端的,这不会反映在powershell

我正在使用.NET Core 3.0,在我的自定义中间件中,我能够使用以下代码获取UserId:

httpContext.User.Identity.IsAuthenticated 
    ? new Guid(httpContext.User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).First().Value)
    : Guid.Empty

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM