HTTP-Authentication 将令牌添加到客户端

发布于 2025-01-20 15:50:49 字数 1999 浏览 1 评论 0原文

我可以设法让 API 服务器向我发送一个令牌。我也可以将令牌存储在 CustomModel 中。 但是我如何将令牌附加到我想使用的 HttpClient 上。

这是我的登录名和函数:

public static async Task<AuthToken> Login()
        {
            var userName = "administrator";
            var passwd = "********";
            var ClientID = "acef4bf9988848a0bff79485d1ac2648";
            var ClientScrt = "********************";
            var url = "https://****.********.****/MobiControl/api/token";

            string body = $"grant_type=password&username={userName}&password={passwd}"; //"grant_type=password&username=Administrator&password=1";
            var payload = new StringContent(body, Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                var authHeader = Encoding.ASCII.GetBytes($"{ClientID}:{ClientScrt}");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeader));

                var result = client.PostAsync(url, payload);

                var token = await result.Result.Content.ReadAsStringAsync();
                AuthToken authToken = JsonConvert.DeserializeObject<AuthToken>(token);

                Console.WriteLine(token);

                ApiHelper.InitializeClient();

                return authToken;
            }
        }

这是我的客户端构造函数:

注释行产生了问题,它给了我两个错误:“非静态字段、方法或属性‘成员’需要对象引用”

 public static class ApiHelper
    {
        public static HttpClient ApiClient { get; set; }

        public static void InitializeClient()
        {
            ApiClient = new HttpClient();

            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //ApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthToken.Token_type, AuthToken.Access_token);

        }
    }

I could manage to get the API-Server sending me a Token. Also i can store the Token in a CustomModel.
But How can i attach the Token to my HttpClient which i want to use.

Here's my Login and function:

public static async Task<AuthToken> Login()
        {
            var userName = "administrator";
            var passwd = "********";
            var ClientID = "acef4bf9988848a0bff79485d1ac2648";
            var ClientScrt = "********************";
            var url = "https://****.********.****/MobiControl/api/token";

            string body = 
quot;grant_type=password&username={userName}&password={passwd}"; //"grant_type=password&username=Administrator&password=1";
            var payload = new StringContent(body, Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                var authHeader = Encoding.ASCII.GetBytes(
quot;{ClientID}:{ClientScrt}");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authHeader));

                var result = client.PostAsync(url, payload);

                var token = await result.Result.Content.ReadAsStringAsync();
                AuthToken authToken = JsonConvert.DeserializeObject<AuthToken>(token);

                Console.WriteLine(token);

                ApiHelper.InitializeClient();

                return authToken;
            }
        }

and here's my Client constructor:

the commented line makes the problem(s), it gives me two faults: "An object reference is required for the nonstatic field, method, or property 'member'"

 public static class ApiHelper
    {
        public static HttpClient ApiClient { get; set; }

        public static void InitializeClient()
        {
            ApiClient = new HttpClient();

            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //ApiClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(AuthToken.Token_type, AuthToken.Access_token);

        }
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文