简体   繁体   中英

404 not found error when trying to initialize Azure AD

I am trying to implement Azure AD on an ASP.NET WebForms application. In the Web.Config, I have added below information:

<add key="ida:RedirectUri" value="https://localhost:44320/" />
<!--Directory_Name.onmicrosoft.com-->
<add key="ida:Tenant" value="https://login.microsoftonline.com/000..." />
<!--App ID URI of service APP-->
<add key="ida:Audience" value="https://login.microsoftonline.com/000../federationmetadata/2007-06/federationmetadata.xml?appid=00000.." />
<!--Client Application Client ID-->
<add key="ida:TrustedCallerClientId" value="000..." />

The Startup.cs file calls Startup.Auth.cs which contains below method.

   public void ConfigureAuth_Azure(IAppBuilder app)
    {
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(
            new WindowsAzureActiveDirectoryBearerAuthenticationOptions
            {
                TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
                {
                    ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
                },
                Tenant = ConfigurationManager.AppSettings["ida:Tenant"]
            }); 
    }

As soon as it hits this code, it throws error:

System.Net.Http.HttpRequestException HResult=0x80131500
Message=Response status code does not indicate success: 404 (Not Found). Source= StackTrace:

Try with this code.

    public void Configuration(IAppBuilder app)
        {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
        // Sets the ClientId, authority, RedirectUri as obtained from web.config
        ClientId = clientId,
        Authority = authority,
        RedirectUri = redirectUrl,
        
        // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
        PostLogoutRedirectUri = redirectUrl,
        Scope = OpenIdConnectScope.OpenIdProfile,
        ResponseType = OpenIdConnectResponseType.IdToken,
        TokenValidationParameters = new TokenValidationParameters()
        {
        ValidateIssuer = false
        },
        // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
        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);
     }

You can follow the below code sample in Github
( https://github.com/azure-cxp-community/Azure-CXP-Community-Engineering/tree/master/src/DeveloperTools/WebApp.OpenIdConnect.Guide )

And also check with this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM