Skip to content

Instantly share code, notes, and snippets.

@jmarmolejos
Created April 8, 2014 21:21
Show Gist options
  • Save jmarmolejos/10194503 to your computer and use it in GitHub Desktop.
Save jmarmolejos/10194503 to your computer and use it in GitHub Desktop.
public ActionResult GetTokenFromThinkTecture()
{
var form = new FormUrlEncodedContent(
new Dictionary<string, string>
{
{OAuth2Constants.GrantType, OAuth2Constants.Password},
{OAuth2Constants.UserName, "user},
{OAuth2Constants.Password, "password"},
{OAuth2Constants.Scope, "https://localhost:44300/OAuth"}
});
var idSrvClient = new HttpClient();
idSrvClient.DefaultRequestHeaders.Authorization =
new BasicAuthenticationHeaderValue(
"test_client",
"test_secret");
var result = idSrvClient.PostAsync(new Uri("https://idsrv/issue/oauth2/token"), form).Result;
var tokenFromThinkTecture = new ContentResult();
tokenFromThinkTecture.Content = result.Content.ReadAsStringAsync().Result;
return tokenFromThinkTecture;
}
// Borrowed from https://github.com/thinktecture/Thinktecture.IdentityModel.45/blob/master/IdentityModel/Thinktecture.IdentityModel/Constants/OAuth2Constants.cs
public static class OAuth2Constants
{
public const string GrantType = "grant_type";
public const string UserName = "username";
public const string Scope = "scope";
public const string Assertion = "assertion";
public const string Password = "password";
public const string Code = "code";
public const string RedirectUri = "redirect_uri";
public static class GrantTypes
{
public const string Password = "password";
public const string AuthorizationCode = "authorization_code";
public const string ClientCredentials = "client_credentials";
public const string RefreshToken = "refresh_token";
public const string JWT = "urn:ietf:params:oauth:grant-type:jwt-bearer";
public const string Saml2 = "urn:ietf:params:oauth:grant-type:saml2-bearer";
}
public static class ResponseTypes
{
public const string Token = "token";
public const string Code = "code";
}
public static class Errors
{
public const string Error = "error";
public const string InvalidRequest = "invalid_request";
public const string InvalidClient = "invalid_client";
public const string InvalidGrant = "invalid_grant";
public const string UnauthorizedClient = "unauthorized_client";
public const string UnsupportedGrantType = "unsupported_grant_type";
public const string UnsupportedResponseType = "unsupported_response_type";
public const string InvalidScope = "invalid_scope";
public const string AccessDenied = "access_denied";
}
}
public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
{
public BasicAuthenticationHeaderValue(string userName, string password)
: base("Basic", EncodeCredential(userName, password))
{ }
private static string EncodeCredential(string userName, string password)
{
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string credential = String.Format("{0}:{1}", userName, password);
return Convert.ToBase64String(encoding.GetBytes(credential));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment