Problem mit OAuth Middleware Persisting Identity.external Cookie in .NET -API mit benutzerdefinierter JWT -AuthentifizieC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Problem mit OAuth Middleware Persisting Identity.external Cookie in .NET -API mit benutzerdefinierter JWT -Authentifizie

Post by Anonymous »

Ich implementiere die OAuth -Authentifizierung (Google & Facebook) in meiner .NET -API mit Entity Framework und JWT -Token. Ich habe bereits mein eigenes lokales Authentifizierungssystem mit JWT Access -Token und aktualisierten Token eingerichtet. Meine eigenen JWT -Token, anstatt das von der OAuth Middleware generierte Cookie/Token zu verwenden. Die folgenden Endpunkte: < /p>

Code: Select all

 [HttpGet("google-login")]
[AllowAnonymous]
public IActionResult GoogleLogin([FromQuery] string returnUrl)
{
var redirectUrl = Url.Action(nameof(GoogleResponse), "Auth", new { returnUrl }, Request.Scheme);
var properties = new AuthenticationProperties { RedirectUri = redirectUrl };
return Challenge(properties, GoogleDefaults.AuthenticationScheme);
}

[HttpGet("signin-google")]
[AllowAnonymous]
public async Task GoogleResponse([FromQuery] string returnUrl)
{
var authenticateResult = await HttpContext.AuthenticateAsync(GoogleDefaults.AuthenticationScheme);

if (!authenticateResult.Succeeded)
return BadRequest("Google authentication failed.");

var claims = authenticateResult.Principal.Identities.FirstOrDefault()?.Claims;
var email = claims?.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
var name = claims?.FirstOrDefault(c => c.Type == ClaimTypes.Name)?.Value;
var key = claims?.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
var ipAddress = HttpContext.Connection.RemoteIpAddress.MapToIPv6().ToString();

if (string.IsNullOrEmpty(email))
return BadRequest("Email not found");

var result = await authService.SignInWithProviderAsync(email, key, ipAddress, "google");

return result.Match(success =>
{
var result = success.Data;

SetAccessTokenInResponse(result.Jwt);
SetRefreshTokenInResponse(result.RefreshToken);
var redirectUri = $"{returnUrl}?access_token={result.Jwt}&refresh_token={result.RefreshToken}";
return Redirect(redirectUri);
}, BadRequest);
}
< /code>
Und dies ist die Einstellung von Programm.cs für OAuth < /p>

builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["JwtConfig:Issuer"],
ValidAudience = builder.Configuration["JwtConfig:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtConfig:Key"]))
};

options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
context.Token = context.Request.Cookies["tmy209w1"];
return Task.CompletedTask;
}
};
}).AddGoogle(options =>
{
options.ClientId = builder.Configuration["Authentication:Google:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:Google:ClientSecret"];
options.CallbackPath = "/signin-google";
options.SaveTokens = false;
}).AddFacebook(options =>
{
options.ClientId = builder.Configuration["Authentication:Facebook:AppId"];
options.ClientSecret = builder.Configuration["Authentication:Facebook:AppSecret"];
options.CallbackPath = "/signin-facebook";
options.SaveTokens = false;
});

Ausgabe

Ich mache nicht Ich möchte das von der OAuth Middleware ausgestellte Token/Cookie verwenden, da ich meine eigene Token -Ausgabe beauftrage. Cookie namens

Code: Select all

identity.external
[/b], und ich bin mir nicht sicher, warum es gespeichert wird und wie man es anhält. H4> Fragen < /strong> < /h4>

Ist Identity.external essentiell? Wenn ja, wie verhindern Sie die identity.externale
Cookie nach der OAuth -Authentifizierung? Zur Verwendung externer Authentifizierung während der Ausgabe meiner eigenen JWT -Token < /p>
< /li>
< /ol>
Es wären Einblicke oder Korrekturen in meinem Ansatz sehr geschätzt. < /P>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post