Asp.net System.Security.SecurityException-获取角色名称

Asp.net System.Security.SecurityException-获取角色名称,asp.net,security,principalpermission,Asp.net,Security,Principalpermission,我在global.asax中实现了一个捕获所有安全异常的方法,如下所示 protected void Application_Error(object sender, EventArgs e) { Exception err = Server.GetLastError(); if (err is System.Security.SecurityException) Response.Redirect("~/Error/Roles.

我在global.asax中实现了一个捕获所有安全异常的方法,如下所示

protected void Application_Error(object sender, EventArgs e)
    {

        Exception err = Server.GetLastError();
        if (err is System.Security.SecurityException)
            Response.Redirect("~/Error/Roles.aspx);

    }
我是否可以访问显示用户权限中缺少的角色名称的属性?例如,err.Rolethattfailed

谢谢你


Etfirfax。

您可以只输出整个堆栈跟踪

i、 e


err.ToString()
将告诉您更多信息。

可以在PermissionState属性中找到该角色。此属性包含需要解析的XML。可以在元素“Identity”中找到角色的名称,该元素具有名为“role”的属性

Exception err = Server.GetLastError();
if (err is System.Security.SecurityException)
{
    var xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(err.PermissionState);
    string roleName = xmlDocument.GetElementsByTagName("Identity")[0].Attributes["Role"].Value;

    ...

    Response.Redirect("~/Error/Roles.aspx);     
}   

谢谢你的回复。我想在用户看到的页面上更具体一些。即“您没有权限XYZ”。err.ToString()将进入我的错误日志,这样我就可以看到发生了什么,但是用户需要看到更友好的东西!