im trying to develop a basic level authorize system but it’s logouts after refreshing the page idk why. I’m kinda beginner so I’m building a minimal project for my company and here I’m stuck at. I can login successfully and my roles are working fine but i didn’t understand why that logout after refreshing the page.
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Authorization;
using ProtaTestTrack2.Services;
using System.Security.Claims;
using System.Threading.Tasks;
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
{
private readonly UserService _userService;
private readonly IHttpContextAccessor _httpContextAccessor;
private ClaimsPrincipal _currentUser;
public CustomAuthenticationStateProvider(UserService userService, IHttpContextAccessor httpContextAccessor)
{
_userService = userService;
_httpContextAccessor = httpContextAccessor;
_currentUser = new ClaimsPrincipal(new ClaimsIdentity());
}
public async Task MarkUserAsAuthenticated(string username, string role)
{
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, username),
new Claim(ClaimTypes.Role, role)
}, CookieAuthenticationDefaults.AuthenticationScheme);
_currentUser = new ClaimsPrincipal(identity);
var authProperties = new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(60)
};
_ = Task.Run(async () =>
{
await _httpContextAccessor.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
_currentUser,
authProperties);
});
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
}
public async Task MarkUserAsLoggedOut()
{
_currentUser = new ClaimsPrincipal(new ClaimsIdentity());
await _httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
}
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
var user = _httpContextAccessor.HttpContext.User;
if (user.Identity.IsAuthenticated)
{
_currentUser = user;
}
return Task.FromResult(new AuthenticationState(_currentUser));
}
public async Task<string> GetUsernameAsync()
{
return _currentUser.Identity.IsAuthenticated ? _currentUser.Identity.Name : null;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" />
<link rel="icon" type="image/ico" href="favicon.ico" />
<HeadOutlet @rendermode="InteractiveServer" />
</head>
<body>
<Routes @rendermode="InteractiveServer"/>
<script src="_framework/blazor.web.js"></script>
<script src="_content/MudBlazor/MudBlazor.min.js"></script>
</body>
</html>
I tried to create new auth templates of mudblazor but didnt work as well.