Last active
August 29, 2015 14:07
-
-
Save dschenkelman/53fd44c66aef2d7e24b5 to your computer and use it in GitHub Desktop.
Xamarin Auth0Client with support for lang in widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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