OAuth客户端调用 在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API ASP.NET Web API与Owin OAuth:调用与用户相关的Web API

public class OAuthClientTest
{
    private HttpClient _httpClient;

    public OAuthClientTest()
    {
        _httpClient = new HttpClient();
        _httpClient.BaseAddress = new Uri("http://openapi.cnblogs.com");
    } 

    [Fact]
    public async Task Get_Accesss_Token_By_Resource_Owner_Password_Credentials_Grant()
    {
        Console.WriteLine(await GetAccessToken());
    }

    private async Task<string> GetAccessToken()
    {
        var clientId = "1234";
        var clientSecret = "5678";

        var parameters = new Dictionary<string, string>();            
        parameters.Add("grant_type", "password");
        parameters.Add("username", "博客园团队");
        parameters.Add("password", "cnblogs.com");

        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
            "Basic",
            Convert.ToBase64String(Encoding.ASCII.GetBytes(clientId + ":" + clientSecret))
            );

        var response = await _httpClient.PostAsync("/token", new FormUrlEncodedContent(parameters));
        var responseValue = await response.Content.ReadAsStringAsync();
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            return JObject.Parse(responseValue)["access_token"].Value<string>();
        }
        else
        {
            Console.WriteLine(responseValue);
            return string.Empty;
        }
    }

      [Fact]
public async Task Call_WebAPI_By_Resource_Owner_Password_Credentials_Grant()
{
    var token = await GetAccessToken();
    _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    Console.WriteLine(await (await _httpClient.GetAsync("/api/users/current")).Content.ReadAsStringAsync());
}

}

1)是的,获取access token的URL就是由TokenEndpointPath决定的。

2)获取access token与Web API没有关系,Web API对应于OAuth中的Resource Server,而获取access token访问的是OAuth中的Authorization Server(CNBlogsAuthorizationServerProvider)。Resource Server(Web API+ Authorize)验证客户端完全是根据access token。

3)是的,在access token的生命周期内,不需要再请求Authorization Server获取token。token不是通过GrantResourceOwnerCredentials生成的。

4)如果不需要设置AuthenticationTicket的属性,当然可以改为context.Validated(oAuthIdentity),这个方法就只是帮你new一下AuthenticationTicket。

5)从测试情况看,去掉await base.GrantResourceOwnerCredentials(context);没影响,但由于不知道基类的这个方法中究竟干了啥,保险起见,就没去掉。

6)ValidateClientAuthentication验证的是客户端(Client),基于client_id与client_secret;GrantResourceOwnerCredentials验证的是用户(ResourceOwner),基于username与password。不能去掉。

在Web API中验证的只是Access Token。
以Resource Owner Password Credentials Grant的方式(grant_type=password)获取的Access Token,不管是受保护的还是与用户相关的API,都可以使用。
以Client Credentials Grant的方式(grant_type= client_credentials)获取的Access Token,只能用于受保护但与用户无关的API。
对于客户端来说,只需持有1个Acess Token即可。

1、OWIN OAuth中的access token是self-contained token,用户标识就存储在access token中,服务端不需要存储,只需解密。这与cookie类似。
2、api通常都用https,如果你对安全性要求高,那就对client_secret进行加密。

 

参考

在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token

ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API

ASP.NET Web API与Owin OAuth:调用与用户相关的Web API

猜你喜欢

转载自www.cnblogs.com/chucklu/p/10364989.html