Skip to content

Instantly share code, notes, and snippets.

@JohnnyJosep
Last active October 2, 2015 11:16
Show Gist options
  • Select an option

  • Save JohnnyJosep/b54cd3beff5fc57172b0 to your computer and use it in GitHub Desktop.

Select an option

Save JohnnyJosep/b54cd3beff5fc57172b0 to your computer and use it in GitHub Desktop.
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Windows.Input;
using Windows.Security.Authentication.Web;
namespace UWP_ExternalLogin.ViewModels
{
public class ExternalViewModel : ViewModelBase
{
//private readonly string URLHttps = "https://localhost:44300/";
private readonly string URLHttps = "https://gf-apiv-1.azurewebsites.net";
private string AccessToken = string.Empty;
private CookieContainer cookies = null;
public ICommand GetExternalLoginsCommand { get; private set; }
public ICommand GetAccessTokenCommand { get; private set; }
private string message;
public string Message
{
get { return message; }
set { Set(ref message, value); }
}
public ObservableCollection<ExternalModel> Externals { get; private set; }
public ExternalViewModel()
{
GetExternalLoginsCommand = new RelayCommand(GetExternalLogins);
GetAccessTokenCommand = new RelayCommand<ExternalModel>(GetAccessToken, (em) => em != null);
Externals = new ObservableCollection<ExternalModel>();
}
private async void GetExternalLogins()
{
Externals.Clear();
cookies = new CookieContainer();
var handler = new HttpClientHandler() { UseCookies = true, CookieContainer = cookies };
using (var client = new HttpClient(handler) { BaseAddress = new Uri(URLHttps) })
{
var response = await client.GetAsync("/api/Account/ExternalLogins?returnUrl=%2F&generateState=true");
var json = await response.Content.ReadAsStringAsync();
var models = JsonConvert.DeserializeObject<List<ExternalModel>>(json);
foreach (var item in models)
{
Externals.Add(item);
}
}
(GetAccessTokenCommand as RelayCommand).RaiseCanExecuteChanged();
}
private async void GetAccessToken(ExternalModel em)
{
Message = "Get permissions...";
var url = string.Format("{0}{1}", URLHttps, em.url);
var wab = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
new Uri(url),
new Uri(URLHttps));
if (wab.ResponseStatus == WebAuthenticationStatus.Success)
{
Message = wab.ResponseData;
}
}
private async void RegisterUser(string email)
{
if (!string.IsNullOrEmpty(AccessToken))
{
using (var client = new HttpClient() { BaseAddress = new Uri(URLHttps) })
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
var data = new Dictionary<string, string>
{
{ "Email", email }
};
var response = await client.PostAsync(
"api/account/registerExternal",
new FormUrlEncodedContent(data));
response.EnsureSuccessStatusCode();
}
}
}
}
public class ExternalModel
{
public string name { get; set; }
public string url { get; set; }
public string state { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment