So speichern Sie Angabensprincipal im Blazor -Server, der in der AuthenticationStateProvider in .NET 9 verwendet wirdC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 So speichern Sie Angabensprincipal im Blazor -Server, der in der AuthenticationStateProvider in .NET 9 verwendet wird

Post by Anonymous »

Ich habe eine Blazor -App, Server, Global, .NET 9. Ich habe mein
erstellt

Code: Select all

ApiAuthenticationStateProvider : AuthenticationStateProvider
, in dem ich GetAuthenticationStateAsync , Login , logout . Theoretisch ist die sicherste Option ein Cookie, aber wenn ich die erstellten Angaben speichern möchte. ProtectedLocalStorage , aber bereits am Start in GetAuthenticationStateEync mit

Code: Select all

await _protectedLocalStorage.GetAsync("token");
< /code>
Ich erhalte den Fehler < /p>

System.inValidoperationException: 'JavaScript -Interop -Aufrufe können derzeit nicht ausgegeben werden. Dies liegt daran, dass die Komponente statisch gerendert wird. Wenn das Vorbereitungsvorgang aktiviert ist, können JavaScript-Interop-Aufrufe nur während der On-Afterrenderasync-Lebenszyklusmethode durchgeführt werden. '

In Microsofts Dokumentation wird das einzige Beispiel darin bestehen, einen Benutzer aus dem Backing-Feld zurückzugeben. />  Ich habe keine Ideen mehr, wie GOTAuthenticationStateAsync () < /Code> ordnungsgemäß implementiert werden kann und wo Informationen über den angemeldeten Benutzer und deren Sitzung gespeichert werden können, was gut wäre, wenn es maximal dauerte. 15 Minuten und wurde erweitert, wenn der Benutzer aktiv war. < /P>
Mein Testcode: < /p>
public sealed class ApiAuthenticationStateProvider
: AuthenticationStateProvider
{
private readonly ILogger _logger;
private readonly ProtectedLocalStorage _protectedLocalStorage;
private readonly IJWTService _jWTService;

private ClaimsPrincipal _getAnnonymousUser => new ClaimsPrincipal(new ClaimsIdentity());

public ApiAuthenticationStateProvider(ILogger logger, ProtectedLocalStorage protectedLocalStorage, IJWTService jWTService)
{
_logger = logger;
_protectedLocalStorage = protectedLocalStorage;
_jWTService = jWTService;
}

public override async Task GetAuthenticationStateAsync()
{
ProtectedBrowserStorageResult result = await _protectedLocalStorage.GetAsync(Definitions.LocalStorage.JWT_TOKEN);
string? authStateString = result.Value;

if (string.IsNullOrEmpty(authStateString))
{
return new AuthenticationState(_getAnnonymousUser);
}

ClaimsPrincipal? claimsPrincipal = _jWTService.GetClaimsPrincipalFromToken(authStateString);

if (claimsPrincipal is not null)
{
return new AuthenticationState(claimsPrincipal);
}
else
{
return new AuthenticationState(_getAnnonymousUser);
}
}

public async Task Login(int idOperator, string login, string actor, List? permissions = null)
{
try
{
List claims = new List
{
new Claim(ClaimTypes.NameIdentifier, idOperator.ToString()),
new Claim(ClaimTypes.Name, login),
new Claim(ClaimTypes.Actor, actor),
};

if (permissions.IsAny())
{
claims.AddRange(permissions.Select(permission => new Claim(Definitions.Claim.PERMISSION, permission)));
}

ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

string jwtToken = _jWTService.GenerateJwt(claimsPrincipal, expirationTimeInMin: 15);

await _protectedLocalStorage.SetAsync(Definitions.LocalStorage.JWT_TOKEN, jwtToken);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimsPrincipal)));
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during login.");
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_getAnnonymousUser)));
return false;
}
}

public async Task Logout()
{
await _protectedLocalStorage.DeleteAsync(Definitions.LocalStorage.JWT_TOKEN);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_getAnnonymousUser)));
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post