tbonejenkins
tbonejenkins

Reputation: 379

RequestContext.Principal.Identity.Name is empty in web api 2 post

I'm new to web api and I seem to be having an issue with getting the name of the signed in user inside of my post method. Im using

RequestContext.Principal.Identity.Name

However, this only seems to be returning an empty string. It works fine in my get method, but not in the post. Here's my entire method

[Route("receive")]
        [HttpPost]
        public HttpResponseMessage Receive(PostmarkInboundMessage message)
        {
            if (message != null)
            {
                // To access message data
                var headers = message.Headers ?? new List<Header>();

                // To access Attachments
                if (message.Attachments != null)
                {
                    var attachments = message.Attachments;

                    var c = new CVService();
                    var user = string.IsNullOrEmpty(RequestContext.Principal.Identity.Name) ? "unknown" : RequestContext.Principal.Identity.Name;

                    c.UpdateLog(user);

                    foreach (var attachment in attachments)
                    {
                        // Access normal members, etc
                        var attachmentName = attachment.Name;

                        // To access file data and save to c:\temp\
                        //if (Convert.ToInt32(attachment.ContentLength) > 0)
                        //{
                        //    byte[] filebytes = Convert.FromBase64String(attachment.Content);
                        //    var fs = new FileStream(attachmentSaveFolder + attachment.Name,
                        //                                   FileMode.CreateNew,
                        //                                   FileAccess.Write,
                        //                                   FileShare.None);
                        //    fs.Write(filebytes, 0, filebytes.Length);
                        //    fs.Close();
                        //}
                    }
                }

                // If we succesfully received a hook, let the call know
                return new HttpResponseMessage(HttpStatusCode.Created);    // 201 Created
            }
            else
            {
                // If our message was null, we throw an exception
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("Error parsing Inbound Message.") });
            }
        }

Any help will be greatly appreciated.

Upvotes: 7

Views: 3980

Answers (1)

Victor Hugo Terceros
Victor Hugo Terceros

Reputation: 3169

Be sure you send the header (token) in both methods GET and POST and also, set the [Authorize] filter in both methods or the controller itself so you will be rejected if the token is not being send

Upvotes: 0

Related Questions