AccessToken ist null bei der Verwendung von OpenIDConnectC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 AccessToken ist null bei der Verwendung von OpenIDConnect

Post by Anonymous »

Ich habe eine Blazor Server-Side-App und in startup.cs Ich habe Folgendes:

Code: Select all

services.AddSingleton();
services.AddOptions(CookieAuthenticationDefaults.AuthenticationScheme)
.Configure((options, store) =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(14);
options.SlidingExpiration = true;
options.SessionStore = store;
});
Ich verwende eine IticketStore Implementierung, um die Cookies mithilfe von MemoryCache zu speichern. und dann die Authentifizierung eingerichtet: < /p>

Code: Select all

services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.Authority = "authority";
options.ClientId = "clientid";
options.ClientSecret = "clientsecret";
options.ResponseType = OpenIdConnectResponseType.Code;
options.ResponseMode = OpenIdConnectResponseMode.FormPost;
options.GetClaimsFromUserInfoEndpoint = true;
options.MapInboundClaims = false;
options.SaveTokens = true;
options.UseTokenLifetime = false;
options.UseSecurityTokenValidator = true;
options.Scope.Add(OpenIdConnectScope.OpenIdProfile);
options.Scope.Add(OpenIdConnectScope.Email);
options.Scope.Add(OpenIdConnectScope.OfflineAccess);
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
services.AddAuthorizationCore();
Und in meinem _host.cshtml Ich mache:

Code: Select all

var tokens = new InitialApplicationState
{
AccessToken = await HttpContext.GetTokenAsync(Token.Access),
IdToken = await HttpContext.GetTokenAsync(Token.Id),
RefreshToken = await HttpContext.GetTokenAsync(Token.Refresh),
};
und der InitialApplicationState wird dann in app.razor .

Code: Select all

var oidcOptions = new OidcClientOptions
{
Authority = "issuer",
ClientId = "clientid",
LoadProfile = false,
Scope = StandardScopes.OpenId
};

var client = new OidcClient(oidcOptions);
var state = await client.GetUserInfoAsync(tokenProvider.AccessToken, ct);
< /code>
Die oben genannte alles funktioniert ein paar Stunden gut. Dann, wenn der Benutzer automatisch authentifiziert ist (seine Authärikations -Cookies werden immer noch im Browser gespeichert), schlägt der Anruf von GetUserInfoasync 
mit Ausnahme fehl, weil TokenProvider.AccessToken null . Wieder. < /p>
Hat jemand dieses Problem gestoßen? Fehlt der obige Code einige Schritte?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post