How to fix GetExternalLoginInfoAsync Always Returns null in ASP.NET MVC
By Tan Lee Published on Feb 18, 2024 1.24K
I've got a problem, the GetExternalLoginInfoAsync method always return null when using single sign on (SSO) with google or facebook
I don't know why, I've tested the local host works perfectly
But when i publish my site on IIS, then sometime it's work ok and some days i can't login with single sign on (SSO)
I don't know why but when i restart the application pool in IIS it's work again.
// GET: /Account/ExternalLoginCallback [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) return RedirectToAction("Login", new { ReturnUrl = ViewBag.ReturnUrl }); // Sign in the user with this external login provider if the user already has a login CustomUser user = await AppService.CustomUserStore.FindByEmailAsync(loginInfo.Email); if (user != null) await AppService.CustomUserStore.Insert(user.UserName, loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey); var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent: false); switch (result) { case SignInStatus.Success: return RedirectToAction("Index", "Home"); case SignInStatus.Failure: default: ViewBag.ReturnUrl = returnUrl; ViewBag.LoginProvider = loginInfo.Login.LoginProvider; return RedirectToAction("Login", new { ReturnUrl = ViewBag.ReturnUrl }); } }
Any body knows the problem, please help me solve. Thanks in advance !
Answer
You can clear the session before calling the ExternalLoginCallback method
// POST: /Account/ExternalLogin [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult ExternalLogin(string provider, string returnUrl) { // Request a redirect to the external login provider ControllerContext.HttpContext.Session.RemoveAll(); return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl })); }
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Essential Tips for Securing Your ASP.NET Website
- Top Security Best Practices for ASP.NET
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
Categories
Popular Posts
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
Modernize Material UI Admin Dashboard Template
Nov 19, 2024
Base Responsive Material UI Admin Dashboard Template
Nov 18, 2024
Modular Admin Template
Nov 14, 2024