gpt4 book ai didi

c# - 在 ASP.NET Web 窗体中使用 ASP.NET Web API 时出现 "Method Not Allowed"错误

转载 作者:太空狗 更新时间:2023-10-29 20:23:35 24 4
gpt4 key购买 nike

我正在尝试使用此处给出的代码实现 HMAC 身份验证:http://bitoftech.net/2014/12/15/secure-asp-net-web-api-using-api-key-authentication-hmac-authentication/ .

我将此代码集成到我的 ASP.NET Web 窗体应用程序中。我创建了一个名为“HMACAPI”的文件夹,并在其中添加了 Controller 和过滤器。我还安装了所有必需的 Nuget 包。这就是我实现服务方法的方式:

[HMACAuthentication]
[RoutePrefix("api/forms")]
public class FormsController : ApiController
{
[Route("")]
public IHttpActionResult Get()
{
ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;

var Name = ClaimsPrincipal.Current.Identity.Name;

return Ok("test");
}

[Route("")]
public IHttpActionResult Post(string order)
{
return Ok(order);
}
}

这是我的 API 路由配置:

GlobalConfiguration.Configure(APIWebFormsProject.API.WebApiConfig.Register);

但是当我使用 client.PostAsJsonAsync() 时,它显示 Method Not Allowed 错误。我尝试了各种 SO 问题,但他们的回答都没有帮助。

我尝试了什么:

  1. 删除了 WebDAV 模块。

  2. 向 post 方法添加了 [HttpPost] 属性。

我正在使用“http://localhost:56697/api/forms/”URL 来访问 API。但我也尝试了“http://localhost:56697/api/forms”和“http://localhost:56697/api/forms/test”。

更新

正如 Obsidian Phoenix 所建议的,我能够在没有 [HMACAuthentication] 属性的情况下运行它。但我想通过 HMAC 身份验证来实现这一点。那么,这可能是什么原因呢?

最佳答案

我猜你的问题是将 HTTP POST 发送到端点 (api/forms),这与 HMACAuth 属性无关,对吧?

如果是这种情况,则不要将订单作为字符串发送,它应该作为包含字符串属性的 POCO 对象,如下所示应该有效:

public class OrderModel
{
public string Order { get; set; }
}

关于c# - 在 ASP.NET Web 窗体中使用 ASP.NET Web API 时出现 "Method Not Allowed"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056124/

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