【发布时间】:2020-02-27 03:25:10
【问题描述】:
请帮帮我,我不知道出了什么问题,我已经找了好几个小时了...
我想在 blazor 中的 cookie 上有一个用户登录组件。我需要能够从我的应用程序中所有位置的 cookie 中获取信息。
在我的Startup.cs我已经添加在ConfigureServices:
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddHttpContextAccessor();
services.AddScoped<HttpContextAccessor>();
services.AddHttpClient();
services.AddScoped<HttpClient>();
在端点之前配置
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
然后在应用程序中,我编写了登录代码
public async Task Login()
{
var claims = new List<Claim> {
new Claim(ClaimTypes.Name, "1", ClaimValueTypes.String),
new Claim(ClaimTypes.Surname, "2", ClaimValueTypes.String),
new Claim(ClaimTypes.Country, "3", ClaimValueTypes.String),
new Claim("4", "4.1", ClaimValueTypes.String)
};
var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = false,
AllowRefresh = false
};
var userPrincipal = new ClaimsPrincipal(userIdentity);
await _httpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
userPrincipal);
}
但我的浏览器中没有 cookie,代码/控制台中没有错误。
当我查看状态时
var z= _httpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
userPrincipal).Status
然后我得到一个错误
System.Threading.Tasks.TaskStatus.Faulted 信息。
有人可以帮我吗?我需要一个身份验证和授权系统,可以按需提供有关用户的信息。
我会很感激你的帮助
谢谢
【问题讨论】:
-
发布错误日志,可能会有所帮助
-
我没有收到任何错误。故障信息是来自 signinasync 的状态。
标签: c# authentication cookies authorization blazor