I am (currently) setting up OAuth2 for Discord with https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers
and having troubles.
It functions well until I click 'authorize' in the authorization window of discord. It should supposedly return to my action with route discord-signin which is also defined in the bots OAuth2 settings. But it does not, instead it redirects without error to my Index-Action.
Here is what I define when adding the discord authentication in Program.cs
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddDiscord(o =>
{
o.ClientId = DiscordDefaults.ClientId;
o.ClientSecret = DiscordDefaults.ClientSecret;
o.CallbackPath = new PathString("/signin-discord");
o.AuthorizationEndpoint = DiscordDefaults.AuthorizationEndpoint;
o.TokenEndpoint = DiscordDefaults.TokenEndpoint;
o.UserInformationEndpoint = DiscordDefaults.UserInformationEndpoint;
o.Scope.Add("identify");
o.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id", ClaimValueTypes.UInteger64);
o.ClaimActions.MapJsonKey(ClaimTypes.Name, "username", ClaimValueTypes.String);
o.ClaimActions.MapJsonKey(ClaimTypes.Email, "email", ClaimValueTypes.Email);
});
And here is my controller:
public async Task<IActionResult> Index()
{
return View(await HttpContext.GetExternalProvidersAsync());
}
[HttpPost("~/signin")]
public async Task<IActionResult> Index([FromForm] string provider)
{
// Note: the "provider" parameter corresponds to the external
// authentication provider choosen by the user agent.
if (string.IsNullOrWhiteSpace(provider))
{
return BadRequest();
}
if (!await HttpContext.IsProviderSupportedAsync(provider))
{
return BadRequest();
}
// Instruct the middleware corresponding to the requested external identity
// provider to redirect the user agent to its own authorization endpoint.
// Note: the authenticationScheme parameter must match the value configured in Startup.cs
var challengeResult = Challenge(new AuthenticationProperties { RedirectUri = "/" }, provider);
return challengeResult;
}
[Route("signin-discord")]
public async Task<IActionResult> DiscordSignIn(string code)
{
Debug.WriteLine(code);
return RedirectToAction(nameof(LoginSuccess));
}
Any ideas why it fails?