C# azure web app中HttpContext中的用户配置文件为空
我正在将azure web app服务与身份提供商Auth0一起使用。 我做到了这一点,但每当我登录azure上托管的网站时,用户配置文件都不会加载到HttpContext中。 这在IIS Express中本地工作。 知道我遗漏了什么吗 这是我获取已登录用户配置文件的代码:C# azure web app中HttpContext中的用户配置文件为空,c#,azure,authentication,azure-web-app-service,auth0,C#,Azure,Authentication,Azure Web App Service,Auth0,我正在将azure web app服务与身份提供商Auth0一起使用。 我做到了这一点,但每当我登录azure上托管的网站时,用户配置文件都不会加载到HttpContext中。 这在IIS Express中本地工作。 知道我遗漏了什么吗 这是我获取已登录用户配置文件的代码: [Inject] private IHttpContextAccessor HttpContextAccessor { get; set; } private string loginId; ///
[Inject]
private IHttpContextAccessor HttpContextAccessor { get; set; }
private string loginId;
/// <summary>
/// On initialized
/// </summary>
/// <returns></returns>
protected override async Task OnInitializedAsync()
{
await LoadInvoiceTypes();
this.loginId = HttpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
}
[注入]
专用IHttpContextAccessor HttpContextAccessor{get;set;}
私有字符串loginId;
///
///初始化时
///
///
受保护的重写异步任务OnInitializedAsync()
{
等待LoadInvoiceTypes();
this.loginId=HttpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c=>c.Type=“http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier)价值;
}
我在网站上做了一点登录,看起来HttpContext是空的
提前谢谢 请先查看我的测试结果。 下面是我的测试代码
public string test()
{
var obj = _httpContextAccessor.HttpContext;
this.loginId = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
return loginId;
}
你可以通过gif看到我的测试结果
我所尝试的只是添加services.AddSingleton()代码>在Startup.cs
中
更多细节,你可以参考博客,它对我有用
您也可以,希望我的答案能对您有所帮助。请先查看我的测试结果。
下面是我的测试代码
public string test()
{
var obj = _httpContextAccessor.HttpContext;
this.loginId = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
return loginId;
}
你可以通过gif看到我的测试结果
我所尝试的只是添加services.AddSingleton()代码>在Startup.cs
中
更多细节,你可以参考博客,它对我有用
您也可以,希望我的回答能对您有所帮助。显然,blazor建议您使用AuthenticationStateProvider。
这给了我azure中的登录用户,正如我所期望的那样
protected string loginId;
[Inject]
private AuthenticationStateProvider AuthProvider { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await AuthProvider.GetAuthenticationStateAsync();
this.loginId = authState.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
await base.OnInitializedAsync();
}
感谢您的输入。显然,blazor建议您使用AuthenticationStateProvider。
这给了我azure中的登录用户,正如我所期望的那样
protected string loginId;
[Inject]
private AuthenticationStateProvider AuthProvider { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await AuthProvider.GetAuthenticationStateAsync();
this.loginId = authState.User.Claims.FirstOrDefault(c => c.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier")?.Value;
await base.OnInitializedAsync();
}
谢谢你的意见。嗨,杰森,谢谢你的意见,但在本地一切都正常。当我发布到azure时,HttpContext是空的。嗨,Jason,谢谢你的输入,但在本地一切正常。当我发布到azure时,HttpContext是空的。我建议你下次提问时,应该清楚地说明问题,从帖子的开始到结尾都没有提到blazor,在回答答案时提到了blazor,这是非常不友好的。我建议你下次提问时,这个问题应该说清楚,从帖子的开始到结尾都没有提到blazor,而且在回答问题时提到了blazor,这是非常不友好的。