【发布时间】:2023-02-21 03:08:32
【问题描述】:
首先,如果这个话题已经被讨论过,我很抱歉。我无法在网上找到我想要的东西,所以这就是我这样做的原因。
标题解释得很好,当我尝试访问它时,HttpContextAccessor 为空。 我在 Program.cs 中注册了这样的服务:
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddScoped<CustomStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<CustomStateProvider>());
我正在使用它在身份验证时将用户数据保存在 cookie 中。
我在 CustomStateProvider 类中注入了 IHttpContextAccessor :
[Inject]
public IHttpContextAccessor HttpContextAccessor { get; set; }
但是,当我启动该应用程序时,我在这一行收到 System.NullReferenceException:
var cookie = HttpContextAccessor.HttpContext.Request.Cookies["CurrentUser"];
这是完整的 CustomStateProvider 类:
public class CustomStateProvider : AuthenticationStateProvider
{
[Inject]
public IHttpContextAccessor HttpContextAccessor { get; set; }
private readonly IAuthService _authService;
private CurrentUser _currentUser;
public CustomStateProvider(IAuthService authService)
{
this._authService = authService;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var identity = new ClaimsIdentity();
try
{
var userInfo = GetCurrentUser();
if (userInfo.IsAuthenticated)
{
var claims = new[] { new Claim(ClaimTypes.Name, _currentUser.UserName) }.Concat(_currentUser.Claims.Select(c => new Claim(c.Key, c.Value)));
identity = new ClaimsIdentity(claims, "Server authentication");
}
}
catch (HttpRequestException ex)
{
Console.WriteLine("Request failed:" + ex);
}
return new AuthenticationState(new ClaimsPrincipal(identity));
}
public async Task Login(ConnexionModel loginParameters)
{
_authService.Login(loginParameters);
// No error - Login the user
var user = _authService.GetUser(loginParameters.UserName);
_currentUser = user;
var cookieOptions = new CookieOptions
{
Expires = DateTime.Now.AddDays(7),
HttpOnly = true
};
HttpContextAccessor.HttpContext.Response.Cookies.Append("CurrentUser", JsonConvert.SerializeObject(_currentUser), cookieOptions);
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public async Task Logout()
{
_currentUser = null;
HttpContextAccessor.HttpContext.Response.Cookies.Delete("CurrentUser");
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public async Task Register(InscriptionModel registerParameters)
{
_authService.Register(registerParameters);
// No error - Login the user
var user = _authService.GetUser(registerParameters.UserName);
_currentUser = user;
var cookieOptions = new CookieOptions
{
Expires = DateTime.Now.AddDays(7),
HttpOnly = true
};
HttpContextAccessor.HttpContext.Response.Cookies.Append("CurrentUser", JsonConvert.SerializeObject(_currentUser), cookieOptions);
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public CurrentUser GetCurrentUser()
{
if (_currentUser != null && _currentUser.IsAuthenticated)
{
return _currentUser;
}
else
{
var cookie = HttpContextAccessor.HttpContext.Request.Cookies["CurrentUser"];
if (cookie != null)
{
return JsonConvert.DeserializeObject<CurrentUser>(cookie);
}
}
return new CurrentUser();
}
}
}
我想我没有正确注册服务,我尝试弄乱 Program.cs 但无法解决问题。我对 Blazor 很陌生,所以我对这类东西了解不多。
感谢您的帮助。
【问题讨论】:
标签: c# service blazor blazor-server-side