使用授权中间件而不是授权属性 ASP NET Core
Use Authorization middleware instead of AuthorizationAttribute ASPNET Core
我有一个专用的 IdServer 运行,它具有其他应用程序将引导未经身份验证的用户进入的登录页面。
我当前的管道是:
app.UseCookieAuthentication
app.UseOpenIdConnectAuthentication
app.UseDefaultFiles // because it is a SPA app
app.UseStaticFiles // the SPA app
所以所有教程都说要在您的控制器上使用 [Authorize]
...
但是,我希望 middle 能够授权我所有的控制器和静态文件。
那么我该如何编写一个中间件来处理它。
我当前的设置是:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<IdentityServerAppOptions> identityServerAppOptions)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
var serverAppOptions = identityServerAppOptions.Value;
loggerFactory.CreateLogger("Configure").LogDebug("Identity Server Authority Configured: {0}", serverAppOptions.Authority);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = serverAppOptions.Authority,
RequireHttpsMetadata = false,
ClientId = "Video",
SaveTokens = true
});
app.Use(async (context, next) =>
{
var authService = context.RequestServices.GetRequiredService<IAuthorizationService>();
if (!await authService.AuthorizeAsync(context.User, context, "Api"))
{
// This is as far as I have got, here we should boot them to IdServer
}
});
app.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = new List<string> { "index.html" },
RequestPath = new PathString("")
});
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "no-cache");
}
});
app.UseMvc();
}
只需添加 AuthenticationManager
Challenge
:
app.Use(async (context, next) =>
{
var authService = context.RequestServices.GetRequiredService<IAuthorizationService>();
if (!await authService.AuthorizeAsync(context.User, context, "Api"))
{
await context.Authentication.ChallengeAsync("oidc");
}
else
{
await next();
}
});
我有一个专用的 IdServer 运行,它具有其他应用程序将引导未经身份验证的用户进入的登录页面。
我当前的管道是:
app.UseCookieAuthentication
app.UseOpenIdConnectAuthentication
app.UseDefaultFiles // because it is a SPA app
app.UseStaticFiles // the SPA app
所以所有教程都说要在您的控制器上使用 [Authorize]
...
但是,我希望 middle 能够授权我所有的控制器和静态文件。
那么我该如何编写一个中间件来处理它。
我当前的设置是:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions<IdentityServerAppOptions> identityServerAppOptions)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
var serverAppOptions = identityServerAppOptions.Value;
loggerFactory.CreateLogger("Configure").LogDebug("Identity Server Authority Configured: {0}", serverAppOptions.Authority);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
Authority = serverAppOptions.Authority,
RequireHttpsMetadata = false,
ClientId = "Video",
SaveTokens = true
});
app.Use(async (context, next) =>
{
var authService = context.RequestServices.GetRequiredService<IAuthorizationService>();
if (!await authService.AuthorizeAsync(context.User, context, "Api"))
{
// This is as far as I have got, here we should boot them to IdServer
}
});
app.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = new List<string> { "index.html" },
RequestPath = new PathString("")
});
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers.Append("Cache-Control", "no-cache");
}
});
app.UseMvc();
}
只需添加 AuthenticationManager
Challenge
:
app.Use(async (context, next) =>
{
var authService = context.RequestServices.GetRequiredService<IAuthorizationService>();
if (!await authService.AuthorizeAsync(context.User, context, "Api"))
{
await context.Authentication.ChallengeAsync("oidc");
}
else
{
await next();
}
});