none
implementing IClientMessageInspector to add custom header - access to HttpContext.User? RRS feed

  • Question

  • After reading about flowing the HttpContext.Current.User (IPrincipal) to a WCF Service, I'm attempting create a custom soap header which will contain my custom principal.  In global.asax in MvcApplication_PreRequestHandlerExecute, I instantiate a new CustomPrincipal which implements IPrincipal, and assign the new CustomPrincipal object to Context.User.  My understanding is that assigning to Context.User effectively assigns to the thread.

    Then I created a new inspector class that implements IClientMessageInspector - my intent is to add a new custom soap header to every message being sent from a client - that header will contain the CustomPrincipal.  In function BeforeSendRequest, I retrieve Threading.Thread.CurrentPrincipal which gives back a GenericPrincipal, but it won't allow me to cast the result to my CustomPrincipal type, so I'm not able to get at the CustomPrincipal properties that I need.  I get this error when I try to cast it:

    Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to type 'TestWcfIdentity.Common.CustomPrincipal'.

    I've implemented a custom endpoint behavior and extension which adds the client message inspector to the endpoint.

    In IClientMessageInspector function BeforeSendRequest, is there a way to get at the CustomPrincipal that I previously assigned to HttpContext.User?

    Thanks for any information.



    • Edited by kicknwing123 Wednesday, January 30, 2013 7:04 PM
    Wednesday, January 30, 2013 6:57 PM

Answers

All replies

  • Hi,

    >>but it won't allow me to cast the result to my CustomPrincipal type

    How do you define CustomPrincipal and how do you assigned to HttpContext.User?

    To troubleshoot the issue, please post some code snippets here to better explain the issue. You can set some breakpoints to inspect those values and their types.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 1, 2013 9:31 AM
    Moderator
  • Not sure that I get the question correctly, but Global.asax is not right place for doing it. Please check the article How to: Create a Custom Principal Identity. Plobably you find the answer to your question.
    Friday, February 1, 2013 8:56 PM
  • I can't go into detail about why Global.asax MvcApplication_PreRequestHandlerExecute *is* the right place to do it, but it is.

    I did solve this problem.  Before, I was just assigning my new CustomPrincipal to Context.User, assuming that action was ultimately assigning my CustomPrincipal to the thread.  But it wasn't.  I added this line:

    Threading.Thread.CurrentPrincipal = customPrincipal

    and that solved it.  After making that change, in the ClientMessageInspector BeforeSendRequest, I am now able to get my customPrincipal by doing this:

    Dim customPrincipal as CustomPrincipal = Threading.Thread.CurrentPrincipal

    Now that I've got my customPrincipal, I can go ahead and create a strongly typed MessageHeader(Of CustomPrincipal), then get an "untyped" header from that, and add the untyped header to request.Headers.  This gets me further along - I'm now able to "flow" my customPrincipal from a client to the WCF Service in a MessageHeader...the problem I now have is how to encrypt my customPrincipal so that it doesn't go over the wire in cleartext.  But I'll post a separate question for that.

    Thanks.


    • Edited by kicknwing123 Wednesday, February 20, 2013 7:04 PM
    Wednesday, February 20, 2013 7:03 PM