0

我正在尝试使用.net 核心中的 EvE online 的 SSO 系统通过 Oauth2 设置登录,并且在 Post 请求阶段发现它是不可能的。这就是我过去使用标准 .net 的方式。有人能帮我转换一下吗?

byte[] encodedKey = Encoding.UTF8.GetBytes(clientId + ":" + clientSecret);
//
HttpWebRequest request = HttpRequestHelper.CreateRequest(new Uri("https://login.eveonline.com/oauth/token"));
request.Host = Host;
request.Headers.Add("Authorization", "Basic " + encodedKey);
request.Method = "POST";
HttpRequestHelper.AddPostData(request, "grant_type=authorization_code&code=" + code);
string response = await requestAsync(request).ConfigureAwait(false);
var result = JsonConvert.DeserializeObject<AuthResponse>(response);
return result;

ps这是我正在寻找的帖子请求格式

POST https://login.eveonline.com/oauth/token HTTP/1.1

Authorization: Basic bG9...ZXQ=
Content-Type: application/x-www-form-urlencoded
Host: login.eveonline.com

grant_type=authorization_code&code=gEyuYF_rf...ofM0
4

2 回答 2

1

我为 EVEOnline SSO 编写了一个模块,在 .net core 1.1 中我使用 HttpClient 发送请求

private readonly HttpClient _client;
public async Task<SsoResponse> AuthenticateAsync(string code)
        {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentNullException("Authentication code is null or empty");

            // Build the link to the SSO we will be using.
            var builder = new UriBuilder(_settings.BaseUrl)
            {
                Path = _settings.TokenPath,
                Query = $"grant_type=authorization_code&code={code}"
            };

            var request = new HttpRequestMessage()
            {
                RequestUri = builder.Uri,
                Method = HttpMethod.Post
            };

            // Set the necessary headers
            request.Headers.Add("Authorization", $"{TokenType.Basic} {_authorizationString}");
            request.Headers.Add("Host", builder.Host);
            request.Headers.Add("User-Agent", _userAgent);

            return await CallSsoAsync<SsoResponse>(request);
        }

private async Task<T> CallSsoAsync<T>(HttpRequestMessage request)
        {
            T response = default(T);
            using (HttpResponseMessage resp = await _client.SendAsync(request))
            {
                // Check whether the SSO answered with 
                // a positive HTTP Status Code
                if (resp.IsSuccessStatusCode)
                {
                    // Deserialize the object into the response model
                    // which will be returned to the application
                    response = JsonConvert.DeserializeObject<T>(await resp.Content.ReadAsStringAsync());
                }
                else
                {
                    // Invalid code receieved, inform the user
                    throw new Exception("The SSO responded with a bad status code: " + resp.StatusCode);
                }
            }

            return response;
        }
于 2017-08-22T20:51:02.340 回答
0

看看http://restsharp.org/。他们还有一个 .net core https://www.nuget.org/packages/RestSharp.NetCore/的 nuget 包。

用法与您的示例非常相似。

于 2017-05-24T10:13:03.747 回答