利用DelegatingHandler实现Web Api 的Api key校验
2021-01-19 12:15
标签:efault uri local task entity parameter header protected tle 基于Querystring提供Api key http://localhost:57967/Api/Values?key=12345 基于Request header体统API key客户端在请求Web Api时可以有以下两种方式提供API key
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-ApiKey","00000");
编写ApiKeyHandler
public class ApiKeyHandler : DelegatingHandler
{
public string Key { get; set; }
public ApiKeyHandler(string key,HttpConfiguration httpConfiguration)
{
this.Key = key;
InnerHandler = new HttpControllerDispatcher(httpConfiguration);
}
protected override Task
配置到特定的路由上去
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: new ApiKeyHandler("12345", GlobalConfiguration.Configuration)
);
利用DelegatingHandler实现Web Api 的Api key校验
标签:efault uri local task entity parameter header protected tle
原文地址:https://www.cnblogs.com/soundcode/p/12156034.html
文章标题:利用DelegatingHandler实现Web Api 的Api key校验
文章链接:http://soscw.com/index.php/essay/44082.html