asp.net-coreauthenticationasp.net-identitymicrosoft-account

How to change default callback of the Microsoft authentication provider login?


In my ASP.Net Core app, I have implemented Microsoft External Login. I now wish to override the default login callback, which is listed by documentation to be https://localhost:5001/signin-microsoft, if of course running on localhost and on that port. The instructions on here then state that the callback override would be something like this: https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback.

I am a bit confused on where the callback is meant to be implemented. At the moment I have ExternalLoginCallback() callback method implemented in a base Controller class. But from looking at the above example, it doesn't look like it should be part of a controller.

Should the callback be inside Startup.cs, a Controller, or some other file I am not currently aware of?


Solution

  • The instructions on here then state that the callback override would be something like this: https://contoso.azurewebsites.net/.auth/login/microsoftaccount/callback.

    That is related to built-in authentication and authorization support in Azure App service . Do you host your app in Azure App service ?

    If yes :

    If you enable the Authentication and authorizationfeature of the app service , that means you are using the built-in authentication and authorization support in Azure . That feature will take over the authentication and authorization of you application , that means authentication and authorization still works even you delete the external Azure AD authentication codes in your application . Then you could just :

    1. Use Authentication and authorizationfeature of the app service , delete the Owin Microsoft Account authentication middleware related codes .

    2. Disable Authentication and authorizationfeature of the app service, use Microsoft Account external login( Microsoft.AspNetCore.Authentication.MicrosoftAccount package) .

    If no :

    Then you should follow document : Microsoft Account external login . You can config the callback url by :

    microsoftOptions.CallbackPath = "/home/about";
    

    But if you are using the ASP.NET Identity template with Microsoft Account external login . After Microsoft authentication , asp.net will check whether user's identity exists in database . Since ASP.NET Core 2.1 and later provides ASP.NET Core Identity as a Razor Class Library. If you want to redirect user to another page after authentication , you can :

    1. Scaffold Identity in ASP.NET Core projects: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio

    2. After that ,modify the redirect url in Areas.Identity.Pages.Account.Login.cshtml.cs:

      public IActionResult OnPost(string provider, string returnUrl = null)
      {
          returnUrl = "/home/contact";
          // Request a redirect to the external login provider.
          var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl });
          var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
          return new ChallengeResult(provider, properties);
      }