Код: Выделить всё
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = FakeAuthHandler.SchemeName;
options.DefaultChallengeScheme = FakeAuthHandler.SchemeName;
}).AddScheme(
FakeAuthHandler.SchemeName,
displayName: FakeAuthHandler.SchemeName,
configureOptions: _ => { }
);
< /code>
FakeAuthHandlerКод: Выделить всё
public class FakeAuthHandler : AuthenticationHandler
{
public const string SchemeName = "TestProvider";
public FakeAuthHandler(IOptionsMonitor options,
ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder) { }
protected override Task HandleAuthenticateAsync()
{
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, "TestExternalUser"),
new Claim(ClaimTypes.Name, "Test User"),
new Claim(ClaimTypes.Email, "testuser@example.com"),
new Claim("urn:google:profile", "https://profiles.google.com/testuser"),
};
var identity = new ClaimsIdentity(claims, SchemeName);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, SchemeName);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}
< /code>
Endpoint looks like this:
[HttpGet("/api/auth/external-login")]
[AllowAnonymous]
public async Task ExternalLogin(string provider, string returnUrl = null)
{
var redirectUrl = _linkGenerator.GetPathByAction(
action: nameof(AuthController.ExternalLoginCallback),
controller: "Auth",
values: new { returnUrl = request.ReturnUrl }
);
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return Challenge(properties, provider);
}
< /code>
I can see in the debugger that FakeAuthHandlerПроблема в том, что/API/Auth/External-Login возвращает 401 и не перенаправляет на путь обратного вызова. Я даже не уверен, как проверить, где проблема. Я что -то пропустил?>
Подробнее здесь: https://stackoverflow.com/questions/796 ... iders-chal
Мобильная версия