Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Authentication with cookies crashes in Blazor

$
0
0

I have the login function and normal use on my Blazor web application. However, sometimes Cookie no longer logs me back in. This happens in an unpredictable way: I code a few lines of web interface code and my Cookies stop working. I copy the new commands, delete the local project, copy a new command from my git, and paste the commands I copied into the same location on the cloned project. And just like that, my Cookie was working normally again. However, recently the above method finally stopped working, even though I cloned from the master branch and not the branch I was working on Cookie still refused to work. What should I do?

Update: I discovered that normally if Cookie works fine, when using F12 on the web and checking there will be 2 cookies saved: .AspNetCore.Antiforgery.w4auo0AhSyM and .AspNetCore.Cookies, but if there is an error then only .AspNetCore.Cookies remains, but now its Secure field has been unchecked.

public async Task<bool> LoginAsync(string username, string password, string unit){    try    {        var user = await FindByInfo(username, password, unit);        if (user != null)        {            var claims = new List<Claim>            {                new Claim(ClaimTypes.Name, user.Name),                new Claim(ClaimTypes.Role, user.Role),                new Claim("unit", user.Unit)            };            var claimsIdentity = new ClaimsIdentity(                claims, CookieAuthenticationDefaults.AuthenticationScheme);            var authProperties = new AuthenticationProperties            {                ExpiresUtc = DateTime.UtcNow.AddHours(1),            };            await _httpContextAccessor.HttpContext.SignInAsync(                CookieAuthenticationDefaults.AuthenticationScheme,                new ClaimsPrincipal(claimsIdentity),                authProperties);            return true;        }        return false;    }    catch(Exception ex)    {        return false;    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>