c# - 什么是 'api_key' 以及如何正确使用它

标签 c# rest servicestack swagger

我对 Restful 服务相当陌生,我刚刚实现了测试代码来让 ServiceStack Restful 服务与 Swagger 插件一起工作,这让我想到了我的问题......

在 swagger-ui/index.html 中有一个用于“api_key”的字段。我知道变量名称是嗯...变量,我也可以随意设置它,但我有点搞不清楚它的用途以及我是否应该使用它。

此外,如果我确实使用它,servicestack 如何在服务器端向我呈现该值?

这是我从文档中启动并运行的测试服务...

    [Api("Hello Web Services")]    
    [Route("/Hello", Summary = @"Noel's ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes")]
    [Route("/Hello/{name}",   Summary = @"N031'5 ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes", Verbs="GET,POST" )] 
    public class Hello
    {
        [ApiMember(Name = "Name", Description = "This is a description", ParameterType = "path", DataType = "string", Verb="GET,POST")]
        public string Name { get; set; }
    }

    public class HelloResponse
    {
        public string Result { get; set; }
    }


    public class HelloService : Service
    {
        public object Any(Hello request)
        {
            return new HelloResponse { Result = "Hello, " + request.Name };
        }
    }

最佳答案

为了回答我自己对 Esker 的后续请求,这里是如何使用 API key ……

public class HelloService : Service
{        
    public object Any(Hello request)        
    {
        string api_key = this.Request.Headers["api_key"];            
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
} 

但还需要一些额外的 javascript 将其包含在 header 中(在 swagger-ui/index.html 内)...

   $(function () {
        $.ajaxSetup({
            beforeSend: function (jqXHR, settings) {
                jqXHR.setRequestHeader("api_key", $("#input_apiKey").val());
            }
        });
    });

我在这个问题的答案中找到了...

How to get Swagger to send API key as a http instead of in the URL

关于c# - 什么是 'api_key' 以及如何正确使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205134/

相关文章:

asp.net-mvc - 使用 MVC Controller 进行 ServiceStack 身份验证

c# - ServiceStack Backbone.Todos 不允许删除 405

c# - Xamarin.Mac - 在窗口标题前显示图标/图像

api - 我是否应该信任使用 GET 方法进行用户身份验证的 API?

servicestack:将 www-authenticate header 添加到错误响应

rest - 使用 Postman 测试 REST 持久性端点

web-services - 设计 Restful 服务

c# - 我怎样才能保存/保持同步对象的内存图与数据库?

C#:ShortDatePattern 给出不正确的结果

c# - 为什么条件方法的返回类型必须为 void?