Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active August 29, 2015 13:55
Show Gist options
  • Save wadewegner/8737853 to your computer and use it in GitHub Desktop.
Save wadewegner/8737853 to your computer and use it in GitHub Desktop.
Sample code for using the WebAuthorizationBroker to authenticate with Salesforce.com using the User Agent auth flow.
private const string AuthorizationEndpointUrl =
"https://login.salesforce.com/services/oauth2/authorize";
private const string ConsumerKey = "YOURCONSUMERKEY";
private const string CallbackUrl = "sfdc://success";
async private void Page_Loaded(object sender, RoutedEventArgs e)
{
var startUrl = Common.FormatAuthUrl(AuthorizationEndpointUrl, ResponseTypes.Token, ConsumerKey,
WebUtility.UrlEncode(CallbackUrl), DisplayTypes.Popup);
var startUri = new Uri(startUrl);
var endUri = new Uri(CallbackUrl);
string result;
try
{
var webAuthenticationResult =
await Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync(
Windows.Security.Authentication.Web.WebAuthenticationOptions.None,
startUri,
endUri);
switch (webAuthenticationResult.ResponseStatus)
{
case Windows.Security.Authentication.Web.WebAuthenticationStatus.Success:
var responseData = webAuthenticationResult.ResponseData;
var responseUri = new Uri(responseData);
var decoder = new WwwFormUrlDecoder(responseUri.Fragment.Replace("#", "?"));
var accessToken = decoder.GetFirstValueByName("access_token");
var refreshToken = decoder.GetFirstValueByName("refresh_token");
var instanceUrl = WebUtility.UrlDecode(decoder.GetFirstValueByName("instance_url"));
result = string.Format("Access Token: {0}\n\nRefresh Token: {1}\n\nInstance URL: {2}",
accessToken, refreshToken, instanceUrl);
break;
case Windows.Security.Authentication.Web.WebAuthenticationStatus.ErrorHttp:
result = webAuthenticationResult.ResponseErrorDetail.ToString();
break;
default:
result = webAuthenticationResult.ResponseData;
break;
}
}
catch (Exception ex)
{
result = ex.Message;
}
lblOutput.Text = result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment