如何实现令牌到 web api 发送请求? [复制]

Posted

技术标签:

【中文标题】如何实现令牌到 web api 发送请求? [复制]【英文标题】:how to implement token to web api send request? [duplicate] 【发布时间】:2020-01-10 04:23:02 【问题描述】:

我的 xamarin.form 应用程序中有这两种方法。我已将 b2c 身份验证添加到我的应用程序中。现在如何更改这些方法以使用令牌。对不起,如果这是一个非常基本的问题,但我太初级了。 谢谢

 private static async Task<string> SendGetRequest(string url)
    
        var httpClient = new HttpClient();
        string responseString = string.Empty;
        var response = await httpClient.GetAsync(url);

        if (response.IsSuccessStatusCode)
        
            responseString = await response.Content.ReadAsStringAsync();
        
        else
        
           ...
        

        return responseString;
    

    private static async Task SendPostRequest(string url, object payLoad)
    
        try
        
            var httpClient = new HttpClient();
            var stringContent = new StringContent(JsonConvert.SerializeObject(payLoad), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            
                ...
            
        
        catch (InvalidOperationException ex)
        
            ...
        
        catch (Exception ex)
        
            ...
        
    

【问题讨论】:

这能回答你的问题吗? Setting Authorization Header of HttpClient 【参考方案1】:

使用授权标头进行身份验证:

private static async Task<string> SendGetRequest(string url)
    
        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationType, accessToken);
        string responseString = string.Empty;
        var response = await httpClient.GetAsync(url);

        if (response.IsSuccessStatusCode)
        
            responseString = await response.Content.ReadAsStringAsync();
        
        else
        
           ...
        

        return responseString;
    

    private static async Task SendPostRequest(string url, object payLoad)
    
        try
        
            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authorizationType, accessToken);
            var stringContent = new StringContent(JsonConvert.SerializeObject(payLoad), Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, stringContent).ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            
                ...
            
        
        catch (InvalidOperationException ex)
        
            ...
        
        catch (Exception ex)
        
            ...
        
    

这里的 authorizationType 类似于“Basic”或“Bearer”,accessToken 是您的令牌。

【讨论】:

以上是关于如何实现令牌到 web api 发送请求? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

Asp.Net Web API JWT 身份验证

Web Api OWIN - 如何在每个请求上验证令牌

如何使用 OAuth 存储和发送带有 API 请求的授权令牌?

如何在 Dotnet Core 中拦截请求 Token Jwt 以发送到其他 API?

Spotify Web API - 没有令牌认证的请求

如何在请求后捕获 REST API 发送的响应令牌?