gpt4 book ai didi

asp.net-mvc - RequestContext.Principal.Identity.Name 在 Web api 2 帖子中为空

转载 作者:行者123 更新时间:2023-12-02 00:04:06 24 4
gpt4 key购买 nike

我是 Web api 的新手,在我的 post 方法中获取登录用户的名称时似乎遇到了问题。我正在使用

RequestContext.Principal.Identity.Name

但是,这似乎只返回一个空字符串。它在我的 get 方法中运行良好,但在 post 中则不然。这是我的整个方法

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

任何帮助将不胜感激。

最佳答案

请确保在 GET 和 POST 方法中发送 header ( token ),并且在这两种方法或 Controller 本身中设置[Authorize]过滤器,以便您如果未发送 token ,将被拒绝

关于asp.net-mvc - RequestContext.Principal.Identity.Name 在 Web api 2 帖子中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com