我可以使用模型绑定验证 HTTP 请求签名令牌和随机数吗?

发布于 2024-11-16 07:57:21 字数 226 浏览 7 评论 0原文

我正在使用 ASP.NET MVC 设置一个端点,可以向该端点发出操作和检索数据的请求(基本上是一个 API)。我使用 2-legged OAuth 模型来验证请求是否使用密钥和签名方法以及随机数表进行签名,以防止劫持。

由于模型绑定在 ASP.NET MVC 中非常方便,我将利用它来消费请求,但我想知道是否可以将签名验证和随机数/时间戳处理直接烘焙到模型绑定器中。这可能吗?这样我就可以重复使用我创建的各种操作的实现。

I am setting up an end-point using ASP.NET MVC to which requests can be made to manipulate and retrieve data (basically, an API). I am using a 2-legged OAuth model to validate that requests be signed using a secret key and signing method as well as a nonce table to prevent hi-jacking.

Since Model Binding is so handy in ASP.NET MVC I am going to take advantage of it to consume requests, but I wonder if I can bake the signature verification and nonce/timestamp handling right into the model binder. Is this possible? That way I can just re-use the implementation on the various Actions that I create.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2024-11-23 07:57:21

我想你应该可以。尝试一下:

public class FooModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            FooModel fooModel = bindingContext.Model as fooModel;
            if (fooModel != null)
            {
               // Do your verification stuff in here
               // Updating any properties of your Model.
               // Or you could retrieve something else entirely and return it if you like
               // Let's pretend we just want to verify the model and set some property or other.
               fooModel.NonceOkay = DoVerification(fooModel);
               fooModel.NextAction = WorkOutWhereToGoNext(fooModel);
               // or whatever
            }
            return fooModel;
        }
    }

DoVerification 可以驻留在您的 ModelBinder 中,但最好驻留在其他地方。

然后将其粘贴到 Global.asax 的 Application_Start 中:

ModelBinders.Binders.Add(typeof(Foo), new FooModelBinder());

I reckon you should be able to. Try this:

public class FooModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            FooModel fooModel = bindingContext.Model as fooModel;
            if (fooModel != null)
            {
               // Do your verification stuff in here
               // Updating any properties of your Model.
               // Or you could retrieve something else entirely and return it if you like
               // Let's pretend we just want to verify the model and set some property or other.
               fooModel.NonceOkay = DoVerification(fooModel);
               fooModel.NextAction = WorkOutWhereToGoNext(fooModel);
               // or whatever
            }
            return fooModel;
        }
    }

DoVerification could live in your ModelBinder, but it might be better for it to live somewhere else.

Then stick this in Application_Start in your Global.asax:

ModelBinders.Binders.Add(typeof(Foo), new FooModelBinder());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文