将承载令牌发送到端点,然后验证此令牌 [英] Sending a bearer token to endpoint, then validate this token

查看:102
本文介绍了将承载令牌发送到端点,然后验证此令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一种向端点发送一些数据的方法,我理解我应该使用承载令牌来验证此调用,该请求是在请求的标头中发送的.

If I have a method that sends some data to an endpoint, I understand I should use a bearer token to authenticate this call, sent in the header of the request.

说我的向/从端点发送/接收数据的方法看起来像这样:

Say my method that sends/receives data to/from the endpoint looks like this:

public async Task<string> PostGetAsync()
        {
            var uri = new Uri("https://localhost:44322/endpoint");

            using (var client = new HttpClient())
            {
                var pairs = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Key", "Value")
                };

                var content = new FormUrlEncodedContent(pairs);
                var response = await client.PostAsync(uri, content);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return "Error posting KeyValue";
                }

                string responseString = response.Content.ReadAsStringAsync().Result;

                JArray json = JArray.Parse(responseString);

                try
                {
                    var returnedJson = json[returnedData];
                    return returnedJson.ToString();
                }
                catch (Exception e)
                {
                    return "Index is out of bounds";
                }
            }
        }

将在该端点处运行的方法称为:

And the method that runs when that endpoint is called it this:

public async Task<JsonResult> endpoint()
        {
            List<Example> items = new List<Example>();

            NameValueCollection nvc = Request.Form;
            string keyString = nvc["Key"];

            try
            {
                items = await GetService.GetList(keyString);
            }
            catch (ServiceException se)
            {

            }

            return Json(items, JsonRequestBehavior.AllowGet);
        }

我如何:

  • 将承载令牌(自定义存储在azure密钥库中)发送到端点.
  • 验证该令牌从端点

我找不到任何适合初学者的文档.

I can't find any beginner friendly docs for doing this.

推荐答案

发送承载令牌就像将HTTP标头添加到以下形式的请求中一样容易: Authorization:Bearer YOURTOKEN .您可以像这样在C#中完成此操作:

Sending a bearer token is as easy as adding an HTTP Header to the request of the form: Authorization: Bearer YOURTOKEN. You can do it in C# like so:

using (var client = new HttpClient())
  {
    client.DefaultRequestHeaders.Authorization =
      new AuthenticationHeaderValue("Bearer", yourTokenString);
    // .. rest of your code

对于服务器端点,您还不清楚如何验证令牌.您提到了Azure KeyVault,但没有说出它的用途.

For the server endpoint, you were pretty unclear how you wish to validate the token. You mention Azure KeyVault but don't say what you are using it for.

通常,服务器通过检查传入令牌的签名来验证传入令牌.此检查需要知道一个秘密.您可以在Azure KeyVault中存储该机密.

Usually the server validates incoming tokens by checking their signature. This check requires knowing a secret. Azure KeyVault is where you might store that secret.

通常,您一次用令牌验证(而不是每个端点)配置服务器框架.然后,您只需指出哪些端点需要令牌验证即可.

Typically you configure your server framework with the token verification once (instead of per end point). You then just indicate which endpoints require token verification.

在整个过程中有许多指南.这是一对:

There are a number of guides that go over the whole process. Here are a couple:

https://blogs.msdn.microsoft.com/webdev/2016/10/27/bearer-token-authentication-in-asp-net-core/https://goblincoding.com/2016/07/03/issuing-and-authenticating-jwt-tokens-in-asp-net-core-webapi-part-i/

如果这还不够,那么您应该发布有关用例和已知知识的更多具体信息.

If this isn't sufficient then you should post more specific information about your use case and what you know.

这篇关于将承载令牌发送到端点,然后验证此令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆