System.Security.SecurityException - Get the role name

431 views Asked by At

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,

ETFairfax.

2

There are 2 answers

1
Graviton On

You can just output the whole stacktrace.

i.e.,

err.ToString() will tell you more info.

0
bytedreamer On

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);     
}