Please help me I don't know what can be wrong, I've been trying to find something for hours...
I want to have a user login component on cookie in blazor. I need possibility to get information from the cookie in all places in my app.
In my Startup.cs
I have added in 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>();
in Configure before endpoint
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
Then in the app, I have made code to make a signin
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);
}
but I get no cookie in my browser, no error in code / console.
When I take a look in the status from
var z= _httpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
userPrincipal).Status
then I get an error
System.Threading.Tasks.TaskStatus.Faulted info.
Can anybody help me? I need an authentication and authorization system that can give me in demand info about user.
I will be grateful for help
Thank You