Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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# DotNetOpenAuth OAuth2_C#_Oauth 2.0_Dotnetopenauth - Fatal编程技术网

C# DotNetOpenAuth OAuth2

C# DotNetOpenAuth OAuth2,c#,oauth-2.0,dotnetopenauth,C#,Oauth 2.0,Dotnetopenauth,因此,我已经进入了OAuth2的第一步,并设法使用从私有API获取访问令牌。因为这只是整个过程中的一小步,我想知道是否有一个框架可以简化过程并管理令牌 我在很多博客上都读过,我已经花了一些时间阅读了文档,但出于某种原因,我不明白如何使用它来实现客户机和某种“令牌管理器/处理器” 有谁能给我提供一个小样本,或者至少列出OAuth2客户机和“令牌管理器/处理程序”所必需的类 谢谢大家! 编辑 这是一个“令牌”类,基本上只存储请求访问令牌时从授权服务器接收的数据: public class Toke

因此,我已经进入了OAuth2的第一步,并设法使用从私有API获取访问令牌。因为这只是整个过程中的一小步,我想知道是否有一个框架可以简化过程并管理令牌

我在很多博客上都读过,我已经花了一些时间阅读了文档,但出于某种原因,我不明白如何使用它来实现客户机和某种“令牌管理器/处理器”

有谁能给我提供一个小样本,或者至少列出OAuth2客户机和“令牌管理器/处理程序”所必需的类

谢谢大家!


编辑

这是一个“令牌”类,基本上只存储请求访问令牌时从授权服务器接收的数据:

public class Token
{
        public Token()
        {
            Issued = DateTime.Now;
        }

        [JsonProperty("access_token")]
        public string AccessToken
        {
            get;
            set;
        }

        [JsonProperty("token_type")]
        public string TokenType
        {
            get;
            set;
        }
// ...
}
Dictionary<string, string> authCred = 
                Configuration.
                GetSection("Authentication:Credentials").
                GetChildren().
                Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).
                ToDictionary(x => x.Key, x => x.Value); 


            var client = new RestClient(Configuration["tokenurl"]);
            RestRequest request = new RestRequest()
            {
                Method = Method.POST
            };
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{authCred["client-id"]}:{authCred["client-secret"]}")));
            request.AddParameter("application/x-www-form-urlencoded", $"grant_type={authCred["grant-type"]}&scope={authCred["scope"]}", ParameterType.RequestBody);
            var response = client.Execute(request);
            Token test = JsonConvert.DeserializeObject<Token>(response.Content);
appsettings.json

{
    "Authentication": {
    "tokenurl": "https://tokenurl.com/give/me/token",
    "Credentials": {
      "client-id": "clientid",
      "client-secret": "clientsecret",
      "grant-type": "client_credentials",
      "scope": "scope"
    }
}
这就是我请求访问令牌的地方:

public class Token
{
        public Token()
        {
            Issued = DateTime.Now;
        }

        [JsonProperty("access_token")]
        public string AccessToken
        {
            get;
            set;
        }

        [JsonProperty("token_type")]
        public string TokenType
        {
            get;
            set;
        }
// ...
}
Dictionary<string, string> authCred = 
                Configuration.
                GetSection("Authentication:Credentials").
                GetChildren().
                Select(x => new KeyValuePair<string, string>(x.Key, x.Value)).
                ToDictionary(x => x.Key, x => x.Value); 


            var client = new RestClient(Configuration["tokenurl"]);
            RestRequest request = new RestRequest()
            {
                Method = Method.POST
            };
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"{authCred["client-id"]}:{authCred["client-secret"]}")));
            request.AddParameter("application/x-www-form-urlencoded", $"grant_type={authCred["grant-type"]}&scope={authCred["scope"]}", ParameterType.RequestBody);
            var response = client.Execute(request);
            Token test = JsonConvert.DeserializeObject<Token>(response.Content);
Dictionary authCred=
配置
GetSection(“身份验证:凭据”)。
GetChildren()。
选择(x=>newkeyvaluepair(x.Key,x.Value))。
ToDictionary(x=>x.Key,x=>x.Value);
var client=new RestClient(配置[“tokenurl]”);
RestRequest请求=新的RestRequest()
{
Method=Method.POST
};
AddHeader(“内容类型”、“应用程序/json”);
request.AddHeader(“Authorization”,Convert.ToBase64String(System.Text.ascienceoding.ASCII.GetBytes($”{authCred[“client id”]}:{authCred[“client secret”]}));
request.AddParameter(“application/x-www-form-urlencoded”,“$”grant_type={authCred[“grant type”]}&scope={authCred[“scope”]}”,ParameterType.RequestBody);
var response=client.Execute(请求);
令牌测试=JsonConvert.DeserializeObject(response.Content);
这实际上效果很好


我的问题是,我是否必须自己构建一个处理此令牌的逻辑,或者是否已经有一个框架为我完成所有工作(我已经阅读了DotNetOpenAuth,但还没有找到一个对我有帮助的示例)?

欢迎使用Stackoverflow!请提供一个代码的工作示例和一个更具体的问题。也许你可以看看。