Skip to content

Instantly share code, notes, and snippets.

@dschenkelman
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dschenkelman/53fd44c66aef2d7e24b5 to your computer and use it in GitHub Desktop.
Save dschenkelman/53fd44c66aef2d7e24b5 to your computer and use it in GitHub Desktop.
Xamarin Auth0Client with support for lang in widget
using System.Reflection;
using Xamarin.Auth;
public class CustomAuth0Client : Auth0.SDK.Auth0Client {
private const string AuthorizeUrl = "https://{0}/authorize?client_id={1}&redirect_uri={2}&response_type=token&connection={3}&scope={4}";
private const string LoginWidgetUrl = "https://{0}/login/?client={1}&redirect_uri={2}&response_type=token&scope={3}";
private readonly string domain;
private readonly string clientId
public CustomAuth0Client (string domain, string clientId) : base(domain, clientId){
this.domain = domain;
this.clientId = clientId;
}
protected override WebRedirectAuthenticator GetAuthenticator(string connection, string scope)
{
// Generate state to include in startUri
var chars = new char[16];
var rand = new Random ();
for (var i = 0; i < chars.Length; i++) {
chars [i] = (char)rand.Next ((int)'a', (int)'z' + 1);
}
var redirectUri = this.CallbackUrl;
var authorizeUri = !string.IsNullOrWhiteSpace (connection) ?
string.Format(AuthorizeUrl, this.domain, this.clientId, Uri.EscapeDataString(redirectUri), connection, scope) :
string.Format(LoginWidgetUrl, this.domain, this.clientId, Uri.EscapeDataString(redirectUri), scope);
var state = new string (chars);
// begin code change
var partialStartUrl = authorizeUri + "&state=" + state;
partialStartUrl += "&lang=" + GetLanguage();
var startUri = new Uri (partialStartUrl);
// end code change
var endUri = new Uri (redirectUri);
var auth = new WebRedirectAuthenticator (startUri, endUri);
auth.ClearCookiesBeforeLogin = false;
return auth;
}
private string GetLanguage(){
// logic to retrieve language
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment