如何使用中间件重定向到自定义403 -ASP.NET
如果身份验证失败了无效的自定义403页面,我正在尝试重定向。 仅当通过LDAP对用户进行身份验证(应属于特定组)时,才能访问网页,
这是我到目前为止所做的:
- 添加的授权中间件
- 添加了授权中间件扩展程序
- 将其配置为startup.cs
- 添加了错误控制器和视图
public async Task Invoke(HttpContext context)
{
// create and search ldap
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapServer);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
var userName = context.User.Identity.Name;
userName = System.IO.Path.GetFileNameWithoutExtension(userName);
// look for SAM account names in groups for the user
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
SearchResult result = mySearcher.FindOne();
var isAuthorized = false;
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(AuthorisedGroup))
{
isAuthorized = true;
}
}
// Return error if the current user is not authorized
if (!isAuthorized)
{
context.Response.StatusCode = 403;
return;
}
// Jump to the next middleware if the user is authorized
await NextRequest.Invoke(context);
}
在startup.cs中配置方法
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
//app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAdAuthorizationMiddleware();//<------- Middleware config
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Fault}/{action=Fault}/{id?}");
});
}
错误控制器
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult StatusCodeHandler(string statusCode)
{
return View($"{statusCode}");
}
}
我添加了2个视图
因此,到达控制器,因此显示标准403页面而不是自定义页面。
感谢您的帮助
I'm trying to redirect if authentication fails to a custom 403 page which Is not working.
The web pages should only be accessible if the user is authenticated via LDAP(should belong to a particular group)
this is what i've done so far:
- Added Authorization middleware
- Added Authorization middleware extension
- Configure it in startup.cs
- Added Error controller and Views
public async Task Invoke(HttpContext context)
{
// create and search ldap
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapServer);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
var userName = context.User.Identity.Name;
userName = System.IO.Path.GetFileNameWithoutExtension(userName);
// look for SAM account names in groups for the user
mySearcher.Filter = "(&(objectClass=user)(|(cn=" + userName + ")(sAMAccountName=" + userName + ")))";
SearchResult result = mySearcher.FindOne();
var isAuthorized = false;
foreach (string GroupPath in result.Properties["memberOf"])
{
if (GroupPath.Contains(AuthorisedGroup))
{
isAuthorized = true;
}
}
// Return error if the current user is not authorized
if (!isAuthorized)
{
context.Response.StatusCode = 403;
return;
}
// Jump to the next middleware if the user is authorized
await NextRequest.Invoke(context);
}
Configure method in startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
//app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAdAuthorizationMiddleware();//<------- Middleware config
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Fault}/{action=Fault}/{id?}");
});
}
Error Controller
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult StatusCodeHandler(string statusCode)
{
return View(quot;{statusCode}");
}
}
I've added 2 views
if I type an invalid url it does show custom 404, but if i set isAuthorized = false
it doesn't get to the controller hence shows the standard 403 page instead of custom page.
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论