/// <summary> /// Log user in to LinkedIn. /// </summary> /// <returns>Boolean indicating login success.</returns> public async Task <bool> LoginAsync() { var user = _storageManager.Get(LinkedInConstants.STORAGEKEYUSER); var credential = _passwordManager.Get(LinkedInConstants.STORAGEKEYACCESSTOKEN); if (!string.IsNullOrEmpty(user) && credential != null) { Tokens.AccessToken = credential.Password; Username = _storageManager.Get(LinkedInConstants.STORAGEKEYUSER); LoggedIn = true; return(true); } string authorizeCode = await GetAuthorizeCodeAsync(Tokens, RequiredPermissions); if (!string.IsNullOrEmpty(authorizeCode)) { var accessToken = await GetAccessTokenAsync(Tokens, authorizeCode); if (!string.IsNullOrEmpty(accessToken)) { Tokens.AccessToken = accessToken; _passwordManager.Store(LinkedInConstants.STORAGEKEYACCESSTOKEN, new PasswordCredential { UserName = LinkedInConstants.STORAGEKEYUSER, Password = accessToken }); _storageManager.Set(LinkedInConstants.STORAGEKEYUSER, LinkedInConstants.STORAGEKEYUSER); return(true); } } LoggedIn = false; return(false); }
public async Task Download(string filename) { var value = Files.First(x => x.Name.Equals(filename)); var file = await storageManager.Get(value.Tag); string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), value.Name); using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { file.CopyTo(fileStream); } await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(fileName) }); }
public static T Get <T>( this IStorageManager storageManager, IKeyBuilder key, FallbackPolicy fallbackPolicy, ISerializer <T, string> serializer = null ) { var serialized = storageManager.Get(key, fallbackPolicy); return((serializer ?? GetDefaultSerializer <T>()) .Deserialize(serialized)); }
/// <summary> /// Log user in to Twitter. /// </summary> /// <returns>Boolean indicating login success.</returns> public async Task <bool> LoginAsync() { var crendetials = _passwordManager.Get("TwitterAccessToken"); var user = _storageManager.Get("TwitterScreenName"); if (!string.IsNullOrEmpty(user) && crendetials != null) { _tokens.AccessToken = crendetials.UserName; _tokens.AccessTokenSecret = crendetials.Password; UserScreenName = user; LoggedIn = true; return(true); } if (await InitializeRequestAccessTokensAsync(_tokens.CallbackUri) == false) { LoggedIn = false; return(false); } string requestToken = _tokens.RequestToken; string twitterUrl = $"{OAuthBaseUrl}/authorize?oauth_token={requestToken}"; Uri startUri = new Uri(twitterUrl); Uri endUri = new Uri(_tokens.CallbackUri); var result = await _authenticationBroker.Authenticate(startUri, endUri); switch (result.ResponseStatus) { case AuthenticationResultStatus.Success: LoggedIn = true; return(await ExchangeRequestTokenForAccessTokenAsync(result.ResponseData)); case AuthenticationResultStatus.ErrorHttp: Debug.WriteLine("WAB failed, message={0}", result.ResponseErrorDetail.ToString()); LoggedIn = false; return(false); case AuthenticationResultStatus.UserCancel: Debug.WriteLine("WAB user aborted."); LoggedIn = false; return(false); } LoggedIn = false; return(false); }
/// <summary> /// Log user in to Weibo. /// </summary> /// <returns>Boolean indicating login success.</returns> public async Task <bool> LoginAsync() { var credentials = _passwordManager.Get(PasswordKey); var uidString = _storageManager.Get(StorageKey); if (long.TryParse(uidString, out var uid) && credentials != null) { _tokens.AccessToken = credentials.Password; Uid = uid; LoggedIn = true; return(true); } string appKey = _tokens.AppKey; string redirectUri = _tokens.RedirectUri; string weiboUrl = $"{OAuthBaseUrl}/authorize?client_id={appKey}&redirect_uri={redirectUri}"; Uri startUri = new Uri(weiboUrl); Uri endUri = new Uri(redirectUri); var result = await _authenticationBroker.Authenticate(startUri, endUri); switch (result.ResponseStatus) { case AuthenticationResultStatus.Success: LoggedIn = true; return(await ExchangeRequestTokenForAccessTokenAsync(result.ResponseData)); case AuthenticationResultStatus.ErrorHttp: LoggedIn = false; return(false); case AuthenticationResultStatus.UserCancel: LoggedIn = false; return(false); } LoggedIn = false; return(false); }
public static T Get <T>( this IStorageManager storageManager, IKeyBuilder key, FallbackPolicy fallbackPolicy, Func <T> callback, ISerializer <T, string> serializer = null ) { serializer = serializer ?? GetDefaultSerializer <T>(); var item = default(T); var serialized = storageManager.Get(key, fallbackPolicy); if (serialized == null || (item = serializer.Deserialize(serialized)) == null) { item = callback(); serialized = serializer.Serialize(item); storageManager.Add(key, serialized); } return(item); }
// Checks if the new Hash has collision. Change it in case and returns a valid unique value private async Task <string> GetHashUrl(string hashUrl, string originalUrl) { int counter = 0; ShortUrl storedShortUrl; do { storedShortUrl = await storageManager.Get(hashUrl); if (storedShortUrl != null) { if (storedShortUrl.OriginalUrl.Equals(originalUrl, StringComparison.InvariantCultureIgnoreCase)) { return(hashUrl); } hashUrl += Convert.ToChar(counter); } } while (storedShortUrl != null); await storageManager.Insert(new ShortUrl { OriginalUrl = originalUrl, ShortnedUrl = hashUrl }); return(hashUrl); }