It is difficult to give a detailed answer without any details about your end, but I have already achieved this by conditional registration:
- external authentication middleware
- global policy requiring authenticated request
it looked something like this:
public class Startup { public Startup(IHostingEnvironment env) { Environment = env; } public IHostingEnvironment Environment { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(x => { if (!Environment.IsDevelopment()) { var authenticatedUserPolicy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); x.Filters.Add(new AuthorizeFilter(authenticatedUserPolicy)); } }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles(); if (!Environment.IsDevelopment()) {
In my case, the authorization filter was applied globally, so an authenticated user is required for each action of the MVC application.
If you have different requirements - small [Authorize] attributes for some actions - then you can probably achieve the same result by changing the way you create associated authorization policies. They generally cannot contain any requirements.
AuthorizationPolicy yourCustomPolicy = null; if (Environment.IsDevelopment()) { yourCustomPolicy = new AuthorizationPolicyBuilder().Build(); } else { yourCustomPolicy = new AuthorizationPolicyBuilder()
source share