Home > Blockchain >  Azure Functions Ad authentication using swagger ui says redirect.html is not found
Azure Functions Ad authentication using swagger ui says redirect.html is not found

Time:11-02

I am using Azure functions V3 Here is my start up.cs public override void Configure(IFunctionsHostBuilder builder) { var configuration = builder.GetContext().Configuration;

        builder.AddSwashBuckle(Assembly.GetExecutingAssembly(), opts =>
        {
            opts.SpecVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
            opts.Title = "My test app";
            
            opts.ConfigureSwaggerGen = x =>
            {
                //custom operation example
                x.CustomOperationIds(apiDesc => apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)
                ? methodInfo.Name
                : new Guid().ToString());

                //custom filter example
                //x.DocumentFilter<RemoveSchemasFilter>();
                //oauth2
                x.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Type = SecuritySchemeType.OAuth2,
                    Flows = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
                            Scopes = new Dictionary<string, string>
                            {
                                { configuration["AzureAd:scope"], "scope" }
                            }
                        },
                        AuthorizationCode = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/authorize", configuration["AzureAd:TenantId"])),
                            TokenUrl = new Uri(string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", configuration["AzureAd:TenantId"])),
                            Scopes = new Dictionary<string, string>
                            {
                                { configuration["AzureAd:scope"], "scope" }
                            }

                        }
                    }
                });
                x.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                     {
                     new OpenApiSecurityScheme
                        {
                        Reference = new OpenApiReference
                        {
                        Type = ReferenceType.SecurityScheme,
                        Id = "oauth2"
                        },
                                Scheme = "oauth2",
                                Name = "oauth2",
                                In = ParameterLocation.Header
                     },
                        new List<string>()
                     }
                });
                
            };
            opts.ClientId = configuration["AzureAd:ClientId"];
            
            opts.OAuth2RedirectPath = "http://localhost:7071/api/swagger/ui/o2c-html";
            //configuration["AzureAd:redirectURI"];
            
        });
        
        builder.Services.AddLogging();

       
    }

The swagger Ui is generated. However when i click on authorize it redirects to redirect.html and says not found. This localhost page can’t be foundNo webpage was found for the web address: http://localhost:7071/api/swagger/ui/o2c-html#

CodePudding user response:

Thanks to @useruser1672994 for providing insights to this. Adding this solved the problem

/// <summary>
    /// This is only needed for OAuth2 client. This redirecting document is normally served
    /// as a static content. Functions don't provide this out of the box, so we serve it here.
    /// Don't forget to set OAuth2RedirectPath configuration option to reflect this route.
    /// </summary>
    /// <param name="req"></param>
    /// <param name="swashBuckleClient"></param>
    /// <returns></returns>
    [SwaggerIgnore]
    [FunctionName("SwaggerOAuth2Redirect")]
    public static Task<HttpResponseMessage> SwaggerOAuth2Redirect(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "swagger/oauth2-redirect")]
        HttpRequestMessage req,
        [SwashBuckleClient] ISwashBuckleClient swashBuckleClient)
    {
        return Task.FromResult(swashBuckleClient.CreateSwaggerOAuth2RedirectResponse(req));
    }
  • Related