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

时间:2009-10-30 11:36:33

标签: 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.aspx);

    }

是否有可以访问的属性显示用户权限中缺少的角色名称? IE浏览器。 err.RoleThatFailed?

Manh,谢谢,

ETFairfax。

2 个答案:

答案 0 :(得分:0)

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

即,

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

答案 1 :(得分:0)

该角色可以在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);     
}   
相关问题