コード例 #1
0
ファイル: OAuthApi.cs プロジェクト: Clancey/SimpleAuth
		protected override async Task<bool> RefreshAccount(Account account)
		{
			lock (locker) {
				if (refreshTask == null || refreshTask.IsCompleted)
					refreshTask = RefreshToken (account);
			}
			return await refreshTask;
		}
コード例 #2
0
ファイル: OAuthApi.cs プロジェクト: MilenPavlov/SimpleAuth
		protected override async Task RefreshAccount(Account account)
		{
			if (refreshTask == null || refreshTask.IsCompleted)
				refreshTask = RefreshToken(account);
			await refreshTask;
		}
コード例 #3
0
ファイル: OAuthApi.cs プロジェクト: Clancey/SimpleAuth
		protected async Task<bool> RefreshToken(Account accaccount)
		{
			try
			{
				var account = accaccount as OAuthAccount;
				if (account == null)
					throw new Exception("Invalid Account");
				var message = new HttpRequestMessage (HttpMethod.Post, TokenUrl) {
					Content = new FormUrlEncodedContent (new Dictionary<string, string>
					{
						{"grant_type","refresh_token"},
						{"refresh_token",account.RefreshToken},
						{"client_id",ClientId},
						{"client_secret",ClientSecret},
					}),
					Headers = {
					{"Accept","application/json"}
				}
				};
				var reply = await Client.SendAsync (message);
				var resp = await reply.Content.ReadAsStringAsync();
				var result = Deserialize<OauthResponse>(resp);
				if (!string.IsNullOrEmpty(result.Error))
				{
					if (string.IsNullOrWhiteSpace(account.RefreshToken) || result.Error == "invalid_grant" || result.ErrorDescription.IndexOf("revoked", StringComparison.CurrentCultureIgnoreCase) >= 0)
					{
						account.Token = "";
						account.RefreshToken = "";
						account.ExpiresIn = 1;
						SaveAccount(account);
						return await Authenticate() != null;
					}
					else
						throw new Exception(result.ErrorDescription);
				}
				if (!string.IsNullOrEmpty(result.RefreshToken))
					account.RefreshToken = result.RefreshToken;
				account.TokenType = result.TokenType;
				account.Token = result.AccessToken;
				account.ExpiresIn = result.ExpiresIn;
				account.Created = DateTime.UtcNow;
				if (account == CurrentAccount)
					await OnAccountUpdated(account);
				CurrentAccount = account;
				SaveAccount(account);
				return true;
			}
			catch (Exception ex)
			{
				OnException(this, ex);
			}
			return false;
		}
コード例 #4
0
ファイル: OAuthApi.cs プロジェクト: MilenPavlov/SimpleAuth
		protected async Task RefreshToken(Account accaccount)
		{
			try
			{
				var account = accaccount as OAuthAccount;
				if (account == null)
					throw new Exception("Invalid Account");

				var reply = await Client.PostAsync(TokenUrl, new FormUrlEncodedContent(new Dictionary<string, string>
				{
					{"grant_type","refresh_token"},
					{"refresh_token",account.RefreshToken},
					{"client_id",ClientId},
					{"client_secret",ClientSecret},
				}));
				var resp = await reply.Content.ReadAsStringAsync();
				var result = Deserialize<OauthResponse>(resp);
				if (!string.IsNullOrEmpty(result.Error))
				{
					if (string.IsNullOrWhiteSpace(account.RefreshToken) || result.Error == "invalid_grant" || result.ErrorDescription.IndexOf("revoked", StringComparison.CurrentCultureIgnoreCase) >= 0)
					{
						account.Token = "";
						account.RefreshToken = "";
						account.ExpiresIn = 1;
						SaveAccount(account);
						await Authenticate();
						return;
					}
					else
						throw new Exception(result.ErrorDescription);
				}
				if (!string.IsNullOrEmpty(result.RefreshToken))
					account.RefreshToken = result.RefreshToken;
				account.TokenType = result.TokenType;
				account.Token = result.AccessToken;
				account.ExpiresIn = result.ExpiresIn;
				account.Created = DateTime.UtcNow;
				if (account == CurrentAccount)
					await OnAccountUpdated(account);
				CurrentAccount = account;
				SaveAccount(account);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex);
			}
		}