Created
November 4, 2014 08:58
-
-
Save JohnnyJosep/3c040e7f916d199af15d to your computer and use it in GitHub Desktop.
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 Newtonsoft.Json; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Collections.ObjectModel; | |
| using System.IO; | |
| using System.Net; | |
| using System.Security; | |
| using System.Threading.Tasks; | |
| using UsersLogin.ViewModel; | |
| using Windows.Web.Http; | |
| namespace UsersLogin | |
| { | |
| public class AuthentificationServices | |
| { | |
| private string baseUri; | |
| public string BaseUri { get { return baseUri; } } | |
| public string AccessToken { get; set; } | |
| public ObservableCollection<ExternalLoginViewModel> ExternalLoginProviders { get; private set; } | |
| public AuthentificationServices(string apiUri) | |
| { | |
| this.baseUri = apiUri; | |
| this.ExternalLoginProviders = new ObservableCollection<ExternalLoginViewModel>(); | |
| } | |
| public async Task GetExternalLoginProviders() | |
| { | |
| string uri = String.Format("{0}/api/Account/ExternalLogins?returnUrl=%2F&generateState=true", this.BaseUri); | |
| var request = WebRequest.CreateHttp(new Uri(uri)); | |
| request.Method = "GET"; | |
| try | |
| { | |
| var response = await request.GetResponseAsync(); | |
| var httpResponse = (HttpWebResponse)response; | |
| string result; | |
| using (var stream = httpResponse.GetResponseStream()) | |
| { | |
| var reader = new StreamReader(stream); | |
| result = await reader.ReadToEndAsync(); | |
| } | |
| var models = JsonConvert.DeserializeObject<List<ExternalLoginViewModel>>(result); | |
| foreach (var provider in models) | |
| { | |
| ExternalLoginProviders.Add(provider); | |
| } | |
| } | |
| catch (SecurityException) | |
| { | |
| throw; | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new InvalidOperationException("Unable to get login providers", ex); | |
| } | |
| } | |
| public void ParseUrlForAccesToken(string url) | |
| { | |
| const string fieldName = "access_token="; | |
| int accessTokenIndex = url.IndexOf(fieldName, StringComparison.Ordinal); | |
| if (accessTokenIndex > -1) | |
| { | |
| int ampersandTokenIndex = url.IndexOf("&", accessTokenIndex, StringComparison.Ordinal); | |
| string tokenField = url.Substring(accessTokenIndex, ampersandTokenIndex - accessTokenIndex); | |
| string token = tokenField.Substring(fieldName.Length); | |
| AccessToken = token; | |
| } | |
| } | |
| public async Task<string> RegisterExternal(string username) | |
| { | |
| ParseUrlForAccesToken(string.Format("{0}/{1}", BaseUri, ExternalLoginProviders[0].Url)); | |
| string uri = String.Format("{0}/api/Account/RegisterExternal", BaseUri); | |
| var model = new RegisterExternalBindingModel { UserName = username }; | |
| var request = WebRequest.CreateHttp(new Uri(uri)); | |
| request.ContentType = "application/json"; | |
| request.Headers["Authorization"] = String.Format("Bearer {0}", AccessToken); | |
| request.Method = "POST"; | |
| string postJson = JsonConvert.SerializeObject(model); | |
| byte[] bytes = new byte[postJson.Length]; | |
| for (int i = 0; i < postJson.Length; i++) | |
| { | |
| bytes[i] = Convert.ToByte(postJson[i]); | |
| } | |
| using (Stream requestStream = await request.GetRequestStreamAsync()) | |
| { | |
| requestStream.Write(bytes, 0, bytes.Length); | |
| } | |
| try | |
| { | |
| WebResponse response = await request.GetResponseAsync(); | |
| HttpWebResponse httpResponse = (HttpWebResponse)response; | |
| string result; | |
| using (Stream responseStream = httpResponse.GetResponseStream()) | |
| { | |
| var reader = new StreamReader(responseStream); | |
| result = await reader.ReadToEndAsync(); | |
| return result; | |
| } | |
| } | |
| catch (SecurityException) | |
| { | |
| throw; | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new InvalidOperationException("Unable to register user", ex); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment