How to stop Authorizing when using localhost for an ASP.NET Core API

Viewed 276

I have an ASP.NET Core Web API that uses Okta to authorize and authenticate. The problem is that the instance of Okta I'm using blocks localhost requests with CORS. If we assume that I can't change that, this is the approach I've taken:

#if !DEBUG
[Authorize]
#endif
public class ApiValuesController : ControllerBase
{
//stuff
}

And in startup.cs:

public void ConfigureServices(IServiceCollection services)
{
     //...
     if (!Environment.IsDevelopment())
     {
         //add authentication
         services.AddAuthentication(options =>
            {
               options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
               options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
               options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
               options.Authority = Configuration["Okta:Authority"];
               options.RequireHttpsMetadata = true;
               options.ClientId = Configuration["Okta:ClientId"];
               options.ClientSecret = "LOL No";
               options.ResponseType = OpenIdConnectResponseType.Code;
               options.GetClaimsFromUserInfoEndpoint = true;
               options.Scope.Add("openid");
               options.Scope.Add("profile");
               options.SaveTokens = true;
               options.TokenValidationParameters = new TokenValidationParameters
               {
                    NameClaimType = "name",
                    RoleClaimType = "groups",
                    ValidateIssuer = true
               };
           });
           services.AddAuthorization();
        }
        //...
    }

    public void Configure(IApplicationBuilder app, IHostEnvironment env)
    {
        //...
        if (!env.IsDevelopment())
        {
            //enable authentication
            app.UseAuthentication();
            app.UseAuthorization();
        }
        //...
    }

But this causes a problem if I need to run my release build locally for any reason. Surely there's a better way for me to be able to run release builds locally without breaking authorization.

Thanks, everyone!

0 Answers
Related