Asp.net core 访问自定义类中Blazor服务器端的AuthenticationStateProvider

Asp.net core 访问自定义类中Blazor服务器端的AuthenticationStateProvider,asp.net-core,blazor,blazor-server-side,Asp.net Core,Blazor,Blazor Server Side,关于如何在自定义类中访问Blazor服务器端的AuthenticationStateProvider,是否有一般性指导?AuthenticationStateProvider是否应作为单例服务添加?用DI还有别的方法吗?我不是说使用授权视图或通过级联参数。我需要能够在自定义类中获取AuthenticationStateProvider.GetAuthenticationStateAync(),而不是控制器、视图等 有什么想法吗?谢谢艾萨克提供的信息,但实际上我能够回答我自己的问题。我的解决方案是

关于如何在自定义类中访问Blazor服务器端的AuthenticationStateProvider,是否有一般性指导?AuthenticationStateProvider是否应作为单例服务添加?用DI还有别的方法吗?我不是说使用授权视图或通过级联参数。我需要能够在自定义类中获取AuthenticationStateProvider.GetAuthenticationStateAync(),而不是控制器、视图等


有什么想法吗?

谢谢艾萨克提供的信息,但实际上我能够回答我自己的问题。我的解决方案是确保我的helper类的作用域是限定的,而不是单例,以便获取authstateprovider的实例

services.AddScoped<Classes.GlobalHelper>();
services.addScope();
然后,我可以像调用任何其他DI一样调用authstateprovider,例如:

public async Task<HttpClient> MyHttpClient()
{
    AuthenticationState _authstate = _authStateProv.GetAuthenticationStateAsync().Result;

    HttpClient http = new HttpClient();
    string signedInUserID = _authstate.User.FindFirst(ClaimTypes.NameIdentifier).Value;
public异步任务MyHttpClient()
{
AuthenticationState\u authstate=\u authStateProv.GetAuthenticationStateAync().Result;
HttpClient http=新的HttpClient();
字符串SignedUserId=\u authstate.User.FindFirst(ClaimTypes.NameIdentifier).Value;

为什么调用Result?您应该等待对GetAuthenticationStateAncy的调用。调用Result错误且阻塞。在我完成之前粘贴代码…耶…等待。对上述问题的答案感兴趣的人: