Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建C#OAuth 2客户端_C#_.net_Oauth_Oauth 2.0 - Fatal编程技术网

创建C#OAuth 2客户端

创建C#OAuth 2客户端,c#,.net,oauth,oauth-2.0,C#,.net,Oauth,Oauth 2.0,我正在用C#创建自己的OAuth 2.0客户端。它确实有效,但我想知道进行授权的函数是否正确和安全。我应该做一些数据加密吗?我见过一些OAuth客户机使用HMACSHA1加密。对不起,我对这件事还不熟悉。代码如下: public string getAccessToken() { string uri = "http://192.168.1.89:8080/oauth/token"; client.DefaultRequestHeaders.Authorization = new

我正在用C#创建自己的OAuth 2.0客户端。它确实有效,但我想知道进行授权的函数是否正确和安全。我应该做一些数据加密吗?我见过一些OAuth客户机使用HMACSHA1加密。对不起,我对这件事还不熟悉。代码如下:

public string getAccessToken()
{
    string uri = "http://192.168.1.89:8080/oauth/token";
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
        string.Format("{0}:{1}", client_id, client_secret))));

    client.BaseAddress = new Uri(uri);

    var parameters = new Dictionary<string, string>();
    parameters["password"] = password;
    parameters["username"] = username;
    parameters["grant_type"] = grant_type;
    parameters["scope"] = scope;
    parameters["client_id"] = client_id;
    parameters["client_secret"] = client_secret;

    var response = client.PostAsync(uri, new FormUrlEncodedContent(parameters)).Result;
    Console.WriteLine((response.StatusCode.ToString()));
    string resultContent = response.Content.ReadAsStringAsync().Result;
    Console.WriteLine(resultContent);

    string s = "access_token\":\"";
    string token = resultContent.Substring(resultContent.IndexOf(s) + s.Length, tokenLength);
    return token;
}
公共字符串getAccessToken()
{
字符串uri=”http://192.168.1.89:8080/oauth/token";
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(
“基本”,转换.tobase64字符串(System.Text.ascienceoding.ASCII.GetBytes(
格式(“{0}:{1}”,客户机id,客户机机密));
client.BaseAddress=新Uri(Uri);
var参数=新字典();
参数[“密码”]=密码;
参数[“用户名”]=用户名;
参数[“授权类型”]=授权类型;
参数[“范围”]=范围;
参数[“客户端id”]=客户端id;
参数[“客户端密码”]=客户端密码;
var response=client.PostAsync(uri,新FormUrlEncodedContent(参数)).Result;
Console.WriteLine((response.StatusCode.ToString());
字符串resultContent=response.Content.ReadAsStringAsync().Result;
Console.WriteLine(结果内容);
字符串s=“访问\令牌\”:\”;
字符串标记=resultContent.Substring(resultContent.IndexOf(s)+s.Length,标记长度);
返回令牌;
}

您是否打算支持特定的流量或拨款类型?不同的授权类型采用不同的参数(例如,客户端凭据和授权流不采用密码,但密码授权类型采用密码)。此外,您可能希望强制使用https,否则,您可能会在internet上以明文形式发送身份验证信息。这是一个密码流。使用http和加密数据怎么样?安全吗?如果要加密http消息体,这意味着服务器需要知道如何解密它。鉴于OAuth2规范不需要特定类型的加密(TLS除外),如果这是密码流的通用客户机,服务器将不知道如何对其进行解密。您是否打算将其用于支持OAuth2密码流的任何服务,或者您编写它是为了与正在实现的特定应用程序接口?我正在编写自己的web应用程序。