Reputation: 983
im learning blazor still - wanted to add something so simple popular Blazored LocalStorage but have some basic trouble / question
code is:
using Microsoft.AspNetCore.Components.Authorization;
using S3.Client.Shared.Services;
using System.Security.Claims;
namespace S3.Client.Helpers
{
public class MyAuthenticationStateProvider : AuthenticationStateProvider
{
private Blazored.LocalStorage.ISyncLocalStorageService l;
public MyAuthenticationStateProvider(Blazored.LocalStorage.ISyncLocalStorageService l)
{
this.l= l;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
AuthenticationState aut;
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, "xxxyyyzzz"),
new Claim(ClaimTypes.Role, "Administrator")
};
ClaimsPrincipal p = new ClaimsPrincipal(new ClaimsIdentity(claims));
l.SetItem<ClaimsPrincipal>("User", p);
ClaimsPrincipal principal = l.GetItem<ClaimsPrincipal>("User");
bool x = p == principal;
if (principal != null && principal.Claims.Count() >0 )
{
aut = new AuthenticationState(principal);
}
else
{
var anonymous = new ClaimsIdentity();
aut = new AuthenticationState(new ClaimsPrincipal(anonymous));
}
return await Task.FromResult(aut);
and please tell me
why x is FALSE ? it should be same object ?
bool x = p.Claims.Count() == principal.Claims.Count()
still false. in debug i see in p._identities =1 a,d in principal._identities =0...
thanks and regards
Upvotes: 0
Views: 1571
Reputation: 48314
This is the answer accepted by the OP in the comments below the question:
The most probable cause of the serialization/deserialization issue is that the JSON serializer used to store/retrieve the data just doesn't like how the Principal
is structured (and thus the serializer somehow ignores the claims collection).
A workaround would be to extract claims to simple data objects (type/value) and store the claim collection as simple array.
Upvotes: 1