0

I am trying to setup a login via Oauth2 using EvE online's SSO system in .net core and am finding it impossible on the Post request stage. This is how I would have done it in the past with standard .net. Is anyone able to help me convert it over?

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;

p.s. this is the post request format I am looking for

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

2 Answers2

1

I wrote a module for EVEOnline SSO, in .net core 1.1 i'm using HttpClient to send the request

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;
        }
Michael
  • 11
  • 2
  • 1
    HI Michael, welcome to Stack Overflow. Would you mind pointing out what was @Kristen doing wrong? Or what the code you pasted does? – Maximo Dominguez Aug 22 '17 at 21:11
  • 1
    Hello Maximo, Basically the code here is how @Kristen would be able to make call to EVEOnline SSO to request an auth_token, So my code from what i've done previous on an Webapp that i have written in .net core – Michael Aug 23 '17 at 01:22
  • Thanks for the clarification, I'm just trying to encourage you to write a better quality answer. – Maximo Dominguez Aug 24 '17 at 03:26
0

Take a look at http://restsharp.org/. They also have a nuget package for .net core https://www.nuget.org/packages/RestSharp.NetCore/.

The usage is quite similiar to your example.

Julien Ambos
  • 2,010
  • 16
  • 29