Why MVC 5 Owin Oauth misses / Account / ExternalLoginCallback action

I am new to MVC 5 authentication. I have currently tried Google Authorization using Owin Code in startup.Auth.cs

var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions { ClientId = "Client-id", ClientSecret = "secret-key", CallbackPath = new PathString("/Account/ExternalLoginCallback"), Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = async context => { context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString())); context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString())); } } }; googleOAuth2AuthenticationOptions.Scope.Add("email"); app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions); 

But for debugging, this does not affect the ExternalLoginCallback action.

 [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback(string returnUrl) 

Does it stop at / Account / ExternalLoginCallback? ReturnUrl =% 2F with a blank white screen. I will not find what is wrong with this. and find a similar question Google Authentication using OWIN Oauth in MVC5 without affecting the ExternalLoginCallback function , but this does not help in my case.

+6
source share
5 answers

This is similar to: Google Authentication using OWIN Oauth in MVC5 without affecting ExternalLoginCallback function

Basically, install the Google app in the developer panel to point to your * / ExternalLoginCallback method.

Leave GoogleProvider with the default callback.

 var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions { ClientId = "MYCLIENTID", ClientSecret = "MYSECRET" }; 

Add a route to handle signin-google in RouteConfig:

 routes.MapRoute( name: "signin-google", url: "signin-google", defaults: new { controller = "[YOURCONTROLLLER]", action = "ExternalLoginCallback"}); 

This should fix the google provider and everyone else.

+2
source

Try it, it might work. His work for my case

 app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() { ClientId = "YourClintId", ClientSecret = "YourSecretKey", CallbackPath = new PathString("/Account/ExternalLoginCallback") }); 
+2
source

I'm having trouble setting up Owin / Katana / Oath etc.

In short ...

In the long ...

Clear your browser history. I tried to find out OWIN / Katana etc. Over the past few days and made many changes to the configuration of Google Developer Console and its code. Sometimes I got a white screen and could not get the debugger to click on the code inside my ExternalLoginCallback() function. Clearing my browser history seems to fix this.

No need to set GoogleOAuth2AuthenticationOptions.CallbackPath , leave it as the default signin-google .

I am testing locally, so I set my Google credentials (replacing the port number with the one you are using!)

Authorized Javascript Origins: " https: // localhost: 44353 "

Authorized redirect URIs: https: // localhost: 44353 / signin-google "and" https: // localhost: 44353 / Account / ExternalLoginCallback "

Too detailed code if anyone is interested

Startup.Auth.cs

 public partial class Startup { public void ConfigureAuth(IAppBuilder app) { var cookieAuthenticationProvider = new CookieAuthenticationProvider(); var cookieAuthenticationOptions = new CookieAuthenticationOptions(); cookieAuthenticationOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie; cookieAuthenticationOptions.LoginPath = new PathString("/Account/Login"); cookieAuthenticationOptions.Provider = cookieAuthenticationProvider; app.UseCookieAuthentication(cookieAuthenticationOptions); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions(); googleOAuth2AuthenticationOptions.ClientId = "TODO : add client id"; googleOAuth2AuthenticationOptions.ClientSecret = "TODO : add secret"; app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions); } } 

The function is executed when the user clicks on my "Sign in to Google". provider will be "google"

 [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public void ExternalLogin(string provider) { var properties = new Microsoft.Owin.Security.AuthenticationProperties(); properties.RedirectUri = Url.Action("ExternalLoginCallback", "Account"); HttpContext.GetOwinContext().Authentication.Challenge(properties, provider); } 

The function that will be executed when the user returns from Google.

 [AllowAnonymous] public async Task<ActionResult> ExternalLoginCallback() { var loginInfo = await Microsoft.Owin.Security.AuthenticationManagerExtensions.GetExternalLoginInfoAsync(HttpContext.GetOwinContext().Authentication); if (loginInfo == null) { throw new NotImplementedException(); } var signInResult = await this.SignInManager.ExternalSignInAsync(loginInfo, false); if (signInResult == Microsoft.AspNet.Identity.Owin.SignInStatus.Success) { return RedirectToAction("Index", "Home"); } if (signInResult == Microsoft.AspNet.Identity.Owin.SignInStatus.RequiresVerification) { // ... } /// etc... } 
+1
source

Try using the same code, but change the CallbackPath to /umbraco/surface/UmbracoIdentityAccount/LinkLoginCallback and register it with the Authorized /umbraco/surface/UmbracoIdentityAccount/LinkLoginCallback URIs in your application.

0
source

You have added the following to map the route to RouteConfig.cs since Google sends a response to your / signin -google domain.

 public static void RegisterRoutes(RouteCollection routes) { ... routes.MapRoute(name: "signin-google", url: "signin-google", defaults: new { controller = "Account", action = "ExternalLoginCallback" }); } 
-1
source

Source: https://habr.com/ru/post/972072/


All Articles