Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

ASP .Net Core 1.1. Web API stopped working after upgrading to .Net Core 2.0

$
0
0

I have a Web API which worked perfectly in .Net Core 1.1 but has stopped working in .Net 2.0. The problem appears to be related to authentication, because I only get it when I try to access endpoints that have the [Authorize] action. I am using Auth0 as my authentication server. To clarify, I didn't upgrade the project, but rather created a new ASP .Net Core 2.0 project, and imported the files from the old project to the new project. This is the error I get as soon as I try to access any restricted endpoint (using Postman):

<body><h1>An unhandled exception occurred while processing the request.</h1><div class="titleerror">UriFormatException: Invalid URI: The hostname could not be parsed.</div><p class="location">System.Uri.CreateThis(string uri, bool dontEscape, UriKind uriKind)</p><div class="titleerror">IOException: IDX10804: Unable to retrieve document from: &#x27;https:///.well-known/openid-configuration&#x27;.</div><p class="location">Microsoft.IdentityModel.Protocols.HttpDocumentRetriever&#x2B;&lt;GetDocumentAsync&gt;d__8.MoveNext()</p><div class="titleerror">InvalidOperationException: IDX10803: Unable to obtain configuration from: &#x27;https:///.well-known/openid-configuration&#x27;.</div><p class="location">Microsoft.IdentityModel.Protocols.ConfigurationManager&#x2B;&lt;GetConfigurationAsync&gt;d__24.MoveNext()</p>

I know there were some breaking changes specifically in the Authorization domain, involving moving some things from Configure to ConfigureServices and vice versa, which I did. This is what my Startup.cs looks like:

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            var corsBuilder = new CorsPolicyBuilder();
            corsBuilder.AllowAnyHeader();
            corsBuilder.AllowAnyMethod();
            corsBuilder.AllowAnyOrigin(); // For anyone access.
            corsBuilder.AllowCredentials();

            services.AddCors(options =>
            {
                options.AddPolicy("SiteCorsPolicy", corsBuilder.Build());
            });

            var config = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutoMapperProfileConfiguration());
            });

            var mapper = config.CreateMapper();
            services.AddSingleton(mapper);

            services.AddMvc();
            services.AddDbContext<InspectionsContext>(options =>
                    options.UseMySql(Configuration.GetConnectionString("InspectionsDatabase")));
            services.Configure<Auth0Settings>(Configuration.GetSection("Auth0"));

            services.AddAuthentication(options =>
            {
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.Audience = Configuration["Auth0:Audience"];
                options.Authority = $"https://{Configuration["Auth0:Domain"]}/";
                options.RequireHttpsMetadata = false;
            });

            services.AddSingleton<IConfiguration>(Configuration);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseCors("SiteCorsPolicy");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Inspections API V1");
            });

            app.UseAuthentication();
            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }


Any ideas?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>