ASP .NET Core IIS托管用户身份名称为空,并且IsAuthenticated = false [英] ASP .NET Core IIS Hosting user identity name is empty and IsAuthenticated=false

查看:188
本文介绍了ASP .NET Core IIS托管用户身份名称为空,并且IsAuthenticated = false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在IIS上运行ASP.NET Core dll(使用AspNetCoreModule)

I'm running ASP .NET Core dll on IIS (with AspNetCoreModule)

使用以前的ASP .NET,我可以使用以下命令获取用户身份名称:

With previous ASP .NET I could get the user identity name with:

HttpContext.Current.User.Identity.Name

因为它是在IIS上下文中直接调用的.

since it was called directly in IIS context.

对于ASP .NET Core,以下结果为空:

With ASP .NET Core the result is null for:

this.Request.User.Identity.Name

似乎IIS没有转发它,或者我的AspNetCore模块没有收到它. 如何接收用户身份名称?

It seems that IIS is not forwarding it or maybe my AspNetCore module doesn't receive it. How can I receive User Identity name?

推荐答案

我解决了这个问题.这是我的调试方式:

I solved the problem. This how I debugged it:

由于我不确定用户凭据是否从IIS传递,因此我决定将代码中的请求HTTP标头打印到日志文件中.答案在那里.我发现IIS可与基本身份验证一起使用.

Since I was not sure if the user credentials are passed from the IIS, I decided to print out Request HTTP headers from the code to log file. The answer was there. I figured out that the IIS works with Basic Authentication.

AspNetCore似乎不支持基本身份验证,因此我添加了一个中间件实现,该中间件实现可解析"Authorization"标头中的信息并从中创建新的用户身份.

It seems that Basic authentication is not supported in AspNetCore, so I added a middleware implementation that parse the information from the 'Authorization' header and create new user identity from it.

我做了自己的实现,但是以下文章对我有很大帮助:

I made my own implementation but the following articles helped me a lot:

https://blog.dangl.我/archive/http-basic-authentication-in-aspnet-core-projects/

https://docs.microsoft.com/zh-CN /aspnet/core/fundamentals/middleware

这篇关于ASP .NET Core IIS托管用户身份名称为空,并且IsAuthenticated = false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆