SSO login in ASP.NET MVC web application can not open Microsoft page for login

Afi M 5 Reputation points
2023-04-28T09:59:20.3366667+00:00

I am trying to add extra login with Microsoft to our project. I have created the app in Azure and did everything as the sample code that Azure provided for me, but when I press the login with Microsoft, it can not be redirected to Microsoft login page and it stays on the login page

Here is my startup code:

public class Startup
{         
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];

    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"];

    static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"];

    string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, System.Configuration.ConfigurationManager.AppSettings["Authority"], tenant);

    public void Configuration(IAppBuilder app)
    {
       app.CreatePerOwinContext(ApplicationDbContext.Create);
       app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
       app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

      app.UseCookieAuthentication(new CookieAuthenticationOptions { });

         app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
        
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
               // AuthenticationMode = AuthenticationMode.Passive,
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUri,
                PostLogoutRedirectUri = redirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
                ResponseType = OpenIdConnectResponseType.CodeIdToken,
                TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false // This is a simplification
                },
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            }
        );
    }

    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context)
    {
        context.HandleResponse();
        context.Response.Redirect("/?errormessage=" + context.Exception.Message);
        return Task.FromResult(0);
    }
}

I have tried the sample code from Azure . enter image description here and here is the part in web.config:

<add key="ClientId" value=" our app client id " />

<add key="Tenant" value="organizations" />

<add key="Authority"

value="https://login.microsoftonline.com/{0}/v2.0" />

<add key="redirectUri" value="https://localhost:44300/"/>

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,468 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.