ServiceStack 在发送客户端之前拦截请求

编程入门 行业动态 更新时间:2024-10-26 20:30:29
本文介绍了ServiceStack 在发送客户端之前拦截请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我为 servicestack 实现了自定义 HMAC 身份验证(示例显示 这里).正如链接底部的建议,我想做这样的客户端:

I have implemented a custom HMAC authentication for servicestack (example shown here). As suggested at the bottom of the link, I wanted to do something like this client side:

var client = new JsonServiceClient(); client.LocalHttpWebRequestFilter += delegate(HttpWebRequest request) { // ContentType still null at this point so we must hard code it // Set these fields before trying to create the token! request.ContentType = ServiceStack.Common.Web.ContentType.Json; request.Date = DateTime.Now; var secret = "5771CC06-B86D-41A6-AB39-9CA2BA338E27"; var token = ApiSignature.CreateToken(request, secret); request.Headers.Add(ApiCustomHttpHeaders.UserId, "1"); request.Headers.Add(ApiCustomHttpHeaders.Signature, token); };

为从此客户端发送到 API 的每个请求附加身份验证标头.然而 LocalHttpWebRequestFilter 似乎不再是 JsonServiceClient 的属性.有没有其他方法可以实现这一目标?

which appends the authentication headers for every request sent to the API from this client. However LocalHttpWebRequestFilter doesn't seem to be a property of JsonServiceClient anymore. Is there an alternative to achieve this?

推荐答案

我找到了解决方案.而不是使用LocalHttpWebRequestFilter,只需要使用RequestFilter.最终的解决方案如下所示:

I found a solution for this. Instead of using LocalHttpWebRequestFilter, need to just use RequestFilter. The final solution looks like this:

var client = new JsonServiceClient(); client.RequestFilter += delegate(HttpWebRequest request) { // ContentType still null at this point so we must hard code it // Set these fields before trying to create the token! request.ContentType = ServiceStack.Common.Web.ContentType.Json; request.Date = DateTime.Now; var secret = "5771CC06-B86D-41A6-AB39-9CA2BA338E27"; var token = ApiSignature.CreateToken(request, secret); request.Headers.Add(ApiCustomHttpHeaders.UserId, "1"); request.Headers.Add(ApiCustomHttpHeaders.Signature, token); };

此处提供解决方案的功劳:ServiceStack JsonServiceClient - 未发送自定义 HTTP 标头

Credit for the solution here: ServiceStack JsonServiceClient - Custom HTTP Headers not sent

更多推荐

ServiceStack 在发送客户端之前拦截请求

本文发布于:2023-11-12 19:11:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   ServiceStack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!