劝告
我有一个blazor服务器应用程序,它使用了一个自定义的authenticationstateprovider。它使用ProtectedSessionStorage保存警告状态。由于我可能想重用这些代码,所以我想将会话密钥传递给构造函数,但我不确定如何做到这一点。
我想的是这样的:Program.cs
. . builder.Services.AddScoped<ProtectedSessionStorage>(); builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>(options => { sessionKey = "abcdef"} ); . .
自定义身份验证状态提供程序.cs
public class CustomAuthenticationStateProvider : AuthenticationStateProvider { private readonly ProtectedSessionStorage _sessionStorage; private readonly string _sessionKey; public CustomAuthenticationStateProvider(ProtectedSessionStorage sessionStorage, sessionKey = "12345") { _sessionStorage = sessionStorage; _sessionKey = sessionKey; } public override async Task<AuthenticationState> GetAuthenticationStateAsync() { try { var userSessionStorageResult = await _sessionStorage.GetAsync<UserSession>("UserSession"); . . . } . . . }
这当然行不通。有人能告诉我我是否走上了正确的轨道吗?