ASP.NET MVC Discord Oauth2 code cannot return viewer is always HTTP 500 error

  Kiến thức lập trình

I have this ASP.NET MVC application using a Discord OAuth2 login to take username and ID. The problem is, no matter what I do, despite the URI PathString /signin-discord correctly directed to a method that returns a .cshtml view, it is instead always resulting in an “HTTP Error 500”.

I honestly have no idea what I did wrong that the URI isn’t properly returning the view.

Here is my configuration for Discord OAuth2 authentication:

[HttpGet("/signin-discord")]
public async Task<IActionResult> ExternalLoginCallback()
{
   var authenticateResult = await HttpContext.AuthenticateAsync("Discord");

   if (!authenticateResult.Succeeded)
   {
       // Handle authentication failure
       return RedirectToAction("Login", "Account"); // Redirect to login page or handle as needed
   }

   // Retrieve Discord user information
   var userId = authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
   var username = authenticateResult.Principal.FindFirst(ClaimTypes.Name)?.Value;

   // Example: Authenticate user locally (store in session or database)
   var claims = new[]
   {
       new Claim(ClaimTypes.NameIdentifier, userId),
       new Claim(ClaimTypes.Name, username),
       // Add additional claims as needed
   };

   var identity = new ClaimsIdentity(claims, "Discord");
   var principal = new ClaimsPrincipal(identity);

   // Sign in the user
   await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

   // Redirect to your viewer page or any desired endpoint
   return View("DrawingPad"); // This will render Views/Drawing/DrawingPad.cshtml

   //return RedirectToAction("DrawingPad", "Drawing");
}

I tried adjusting the Controller Method meant to be invoked by the URI, and setting its signature right at /signin-discord.

[HttpGet("/signin-discord")]
public async Task<IActionResult> ExternalLoginCallback()
//return RedirectToAction("Index", "Home");}

But nothing worked.

New contributor

wellin price is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT