如何处理PrincipalPermission安全异常
我有一个简单的安全方法,
[PrincipalPermission(SecurityAction.Demand, Role = "Administrator")]
protected void lnkClearCache_Click(object sender, EventArgs e)
{
...
}
如果在没有角色的情况下单击此方法,它会按预期生成 System.Security.SecurityException:请求主体权限失败。
。
我使用 ELMAH 来处理错误日志记录,并且我的 global.asax 中有一个自定义 ELMAH 事件,以保留正常工作的状态代码的方式传输到错误页面。
private void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
var customErrorsSection = GetCustomErrorsSection();
var error = args.Entry;
string statusCode = error.Error.StatusCode.ToString();
if (statusCode == "0" && error is security exception)
statusCode = "403";
var errorSection = customErrorsSection.Errors[statusCode];
string redirectUrl = errorSection == null ?
customErrorsSection.DefaultRedirect : errorSection.Redirect;
RespondWithServerError(error.Id, redirectUrl, statusCode);
}
这工作得很好,并重定向到我的错误页面,该页面工作正常,但没有按预期显示内容。我立即收到对错误页面的第二个请求,但这次使用的是 customErrorsSection.DefaultRedirect 的值,该值并非以我所看到的任何方式来自我的代码。
据我所知,这几乎就像当.NET 引发PrincipalPermission 异常然后让整个请求完成时,然后在请求完成后它会丢弃应用程序响应并以默认的自定义错误进行响应。
当我调试时,我确实打破了PrincipalPermission的2个单独的异常,这是否只是.NET的重新抛出,我不确定,但我的.NET代码从未看到第二次抛出,ELMAH也没有。我总是以单个响应、单个错误记录结束,但最终呈现给浏览器的 url 是默认 url,而不是我专门 server.transferred 到的 403 url。如果我浏览到安全的 /location,我会正确地收到 403 错误页面。
I have a simple method that is secured
[PrincipalPermission(SecurityAction.Demand, Role = "Administrator")]
protected void lnkClearCache_Click(object sender, EventArgs e)
{
...
}
If this is clicked without the role, it generates a System.Security.SecurityException: Request for principal permission failed.
as expected.
I use ELMAH to handle logging for my errors, and I have a custom ELMAH event in my global.asax to transfer to the error pages in ways that preserve status codes which works correctly.
private void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
var customErrorsSection = GetCustomErrorsSection();
var error = args.Entry;
string statusCode = error.Error.StatusCode.ToString();
if (statusCode == "0" && error is security exception)
statusCode = "403";
var errorSection = customErrorsSection.Errors[statusCode];
string redirectUrl = errorSection == null ?
customErrorsSection.DefaultRedirect : errorSection.Redirect;
RespondWithServerError(error.Id, redirectUrl, statusCode);
}
This works all well and fine and redirects to my error page which works properly, however instead of displaying the content as expected. I immediately get a second request for the error page but this time using the value of customErrorsSection.DefaultRedirect that does not come from my code in any way that I can see.
As far as I can tell it's almost as if when .NET raises an exception for PrincipalPermission and then lets the entire request complete, then after the request is complete it throws away the application response and instead responds with the default custom error.
When I'm debugging I do break on 2 separate exceptions for PrincipalPermission, whether this is a just a rethrow by .NET I'm not sure but my .NET code never sees the 2nd throw, nor does ELMAH. I always end up with a single response, single error logged, but that the url that finally renders to the browser is the default url and not 403 url that I specifically server.transferred to. If I browse to a /location that is secure I properly get the 403 error page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太清楚问题出在哪里。但我正在使用类似的东西,对我来说这个解决方案(最小化)效果很好。
那是在 global.asax 之外,
但在某些地方我没有重定向,只是使用 try 和 catch 安全异常来显示用户,不允许他执行此类操作。
I don´t excactly know where the problem is. But i´´m using something similar and for me this solution (minimized) works great.
That´s out of global.asax
But on some places I don´´t redirect, and just using try and catch the securityexception to display the user, that he is not allowed to perform such action.