简体   繁体   中英

Blazor cookie authentication

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

Is this a Blazor Server or Blazor WebAssembly?

In any case, you can't use HttpContextAccessor in your App.

This _httpContextAccessor.HttpContext.SignInAsync cannot work because the HttpContext is not available in Blazor Server. Needless to say that HttpContext cannot be created on the browser...

Either use this: services.AddHttpClient();

to use IHttpClientFactory (recommanded)

or this: services.AddScoped<HttpClient>();

It seems to me that you are not familiar with either Blazor or the Authentication and Authorization system of Blazor. I'd suggest you to consult the docs and learn how to use them. They are excellent, and may save you a great deal of hard coding time.

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM