Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null reference in pipeline for PostAuthenticateRequest in MVC 5

I am trying to follow the example from Brock Allen on how to add custom roles to windows roles using claims.(http://brockallen.com/2013/01/17/adding-custom-roles-to-windows-roles-in-asp-net-using-claims/) at the moment, just plugging in the code from their example i get the following error. I am trying this with clean new MVC 5 project that only has Microsoft.IdentityModel added. What am i missing that would cause this error?

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification, Boolean isPostEvent) +22
System.Web.PipelineStepManager.ResumeSteps(Exception error) +1324
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186

here is the code in my global.asax.cs file

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        PostAuthenticateRequest += WebApiApplication_PostAuthenticateRequest;
    }

    void WebApiApplication_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {

            var id = ClaimsPrincipal.Current.Identities.First();
            id.AddClaim(new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender", "potato"));

        }
    }
}
like image 628
Michael Avatar asked Jul 05 '14 01:07

Michael


1 Answers

after more digging it seems to be magic or auto event wrap that makes this work. i don't have to declare an event handler.

namespace WebApplication1
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        void Application_PostAuthenticateRequest()
        {
            if (Request.IsAuthenticated)
            {  
                var id = ClaimsPrincipal.Current.Identities.First();   
            }
        }
    }
}
like image 194
Michael Avatar answered Sep 28 '22 09:09

Michael