开发者

System.Security.SecurityException - Get the role name

I've implemented a catch all security exceptions method in my global.asax like this...

protected void Application_Error(object sender, EventArgs e)
    {

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

    }

Is there a property I can access that shows the name of the role which was missing from the users permissions? Ie. err.RoleThatFailed?

Manh thanks,

ETFair开发者_开发百科fax.


You can just output the whole stacktrace.

i.e.,

err.ToString() will tell you more info.


The role can be found in the PermissionState property. This property contains XML that need to be parsed. The name of the role can found in the element 'Identity', which has an attribute named '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);     
}   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜