Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034940
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:02:08+00:00 2026-05-20T12:02:08+00:00

I have a simple method that is secured [PrincipalPermission(SecurityAction.Demand, Role = Administrator)] protected void

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:02:09+00:00Added an answer on May 20, 2026 at 12:02 pm

    I don´t excactly know where the problem is. But i´´m using something similar and for me this solution (minimized) works great.

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
            Dim er = HttpContext.Current.Error
            If er.GetType.Equals(GetType(System.Security.SecurityException)) Then
                HttpContext.Current.Response.Redirect(FormsAuthentication.LoginUrl & "?ReturnUrl=" & HttpContext.Current.Request.Path)
            End If
        End Sub
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple WebService method that returns database results as a DataSet. This
I have simple web application called App that is secured with Windows Authentication. I
I have a simple web service method that returns a simple java class as
I have a simple class hierarchy where I have a virtual method that is
I have a really simple class with two methods; One that will be called
I have a simple setter method for a property and null is not appropriate
I have a simple extension method on the int type so I can do
I have some very simple code to generate an assembly and invoke a method
I am posting a very simple form using a method I have used frequently
I have a simple Silverlight application that use WCF RIA Services to communicate with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.