HttpContext.Current.User in .Net WebAPI

AppStar

Posted on
Oct 16 2015

I've configured Raygun in my .Net WebAPI application to automatically send all exceptions using RaygunWebApiClient.Attach. How can I send HttpContext.Current.User.Identity as the User of the exception to Raygun?


Jamie Penney

Posted on
Oct 19 2015

Hi,

RaygunWebApiClient.Attach has another overload that takes the config as well as a function to generate the RaygunWebApiClient

void Attach(System.Web.Http.HttpConfiguration config, System.Func<HttpRequestMessage,RaygunWebApiClient> generateRaygunClient)

As an example, you could set up your User like this:

RaygunWebApiClient.Attach(config, request =>
{
  var client = new RaygunWebApiClient();
  MyCustomIdentity identity = (MyCustomIdentity) HttpContext.Current.User.Identity;
  client.UserInfo = new RaygunIdentifierMessage(identity.Id)
  {
    FullName = identity.Name,
    Email = identity.EmailAddress
  };
  return client;
});

That's assuming your Identity has those properties. If it doesn't, the only required one is some form of Id to pass through to the constructor.

Hope this helps.
Jamie


Reply