2015-04-12 6 views
5

Sono un neofita di web api e mi sembra che stia avendo un problema con il nome dell'utente che ha effettuato l'accesso all'interno del mio metodo post. Sto usandoRequestContext.Principal.Identity.Name è vuoto in web api 2 post

RequestContext.Principal.Identity.Name 

Tuttavia, questo sembra solo restituire una stringa vuota. Funziona bene nel mio metodo get, ma non nel post. Ecco il mio intero metodo

[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.") }); 
      } 
     } 

Qualsiasi aiuto sarà molto apprezzato.

risposta

0

Assicurati di inviare l'intestazione (token) in entrambi i metodi GET e POST e anche, impostare il [Autorizza] filtro in entrambi i metodi o il controller stesso in modo da essere respinta se il token non viene invia