【问题标题】:How to protect ASP.NET Web API 2如何保护 ASP.NET Web API 2
【发布时间】:2014-01-02 12:38:07
【问题描述】:

我需要实现 ASP.NET Web API 2

[RoutePrefix("orders")] 
public class OrdersController : ApiController 
{ 
    [Route("{id}")] 
    public Order Get(int id) { } 
    [Route("{id}/approve")] 
    public Order Approve(int id) { } 
} 

我想知道如何保护它?

我们可以为此使用 ASP.NET Identity 吗?

有什么线索吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-identity


    【解决方案1】:

    您可以在类中添加[Authorize] 属性,然后通过标头发送基本身份验证信息。

    【讨论】:

    • 听起来不错!您能否提供任何示例如何send the basic authentication information via the header。还有...应该是 ASP .NET 身份数据库吗?
    • @ClarkKent,这是一个example on how to build the header。是的,基本的 ASP.NET 用户数据库。
    • SimpleSecurity 项目simplesecurity.codeplex.com/SourceControl/latest#AspNetIdentity/… 中有一个完整的示例,其中包含使用ASP.NET Identity 进行基本身份验证的源代码。查看示例的参考应用程序。
    • @KevinJunghans 谢谢!请把它当作一个回答。至少我会 +1 因为它有 How to add basic authentication to Web API's. simplesecurity.codeplex.com
    【解决方案2】:

    您可以使用 ASP.NET 身份创建自定义 AuthorizeAttribute,该身份为您的 Web API 处理基本身份验证。在SimpleSecurity Project 中有一个完整的示例,其中包含有关使用 ASP.NET 身份的基本身份验证的源代码。您可以找到source code for the custom AuthorizeAttribute here。查看参考应用程序以获取有关如何使用它的示例。 source code for the client side code is here。有一个discussion on implementing basic authentication in Web API's here。尽管本文引用了 SimpleMembership,但使用 ASP.NET Identity 的实现应用了相同的主体。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多