Getting the user ID in the Web Api handler when using Cachecow

I have an MVC Web Api project and I am logging all requests and responses with MessageHandler. When an api request arrives, the carrier token in the header allows Asp.Net to do its job and authenticate that user. Therefore, the message handler knows who the user is, and we write it to the log file.

Now, to speed things up, I cache using Cachecow. Therefore, I added the cachecow handler after MessageHandler, and when the second request arrives, everything works fine from the cache point. The controller code never hits and the response is returned from the cache.

However, MessageHandler does not matter for User.Identity, so I cannot say who made the request.

I need to register all requests and determine who made them, even if the code did not get into the controllers.

I think one workaround is to force api requests to pass the carrier token and user id in the header. That way, I can check the user id request and use it to register who made the request.

protected override async Task OutgoingMessageAsync(string correlationId, string requestInfo, byte[] message, string responseTimeMilliseconds)
        {
            await Task.Run(() =>
                Debug.WriteLine(string.Format("{0} - Response: {1}\r\n{2}", correlationId, requestInfo, Encoding.UTF8.GetString(message))));

                    );
        }

User authentication is zero when receiving a response from the cache.

?HttpContext.Current.User.Identity
{System.Security.Claims.ClaimsIdentity}
    [System.Security.Claims.ClaimsIdentity]: {System.Security.Claims.ClaimsIdentity}
    AuthenticationType: null
    IsAuthenticated: false
    Name: null

Any ideas?

+4
source share
3 answers

In the authentication process, specify the object:

System.Threading.Thread.CurrentPrincipal = YourUserInformationObject;

For this object, you need to implement the example "System.Security.Principal.IPrincipal"

    public class YourUserInformation : IPrincipal
    {
       public Int32 Id { get; set; }
       public String NameUser { get; set; }

       public IIdentity Identity { get; private set; }


        public YourUserInformation()
        {
            this.Identity = new GenericIdentity(NameUser ?? "");
        }

        public bool IsInRole(string role) { return false; }
    }

System.Threading.Thread.CurrentPrincipal

    public void Authentication(AuthorizationContext filterContext)
    {
       YourUserInformation user = YourMethodGetUserLogin();                

       System.Threading.Thread.CurrentPrincipal = user ;
    }
0

, HttpContext Request, User.Identity:

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var context = ((HttpContextBase)request.Properties["MS_HttpContext"]);
        var uname = username = context.User.Identity.Name;

        var response = await base.SendAsync(request, cancellationToken);

        return response;
    }

: http://arcware.net/logging-web-api-requests/

!

0

try to login

System.Threading.Thread.CurrentPrincipal

-1
source

Source: https://habr.com/ru/post/1626352/


All Articles