[英]Check if user is already logged in
我正在為Windows Phone 8.1開發一個應用程序,該應用程序將使用Google Blogger Api。 為了獲得授權,我使用的是GoogleWebAuthorizationBroker.AuthorizeAsync()函數,一切正常,我可以使用我的博客進行所有操作(獲取博客/帖子列表,編輯,刪除,添加帖子...)。
當我的應用程序最初打開時,它將調用GoogleWebAuthorizationBroker.AuthorizeAsync()函數,如果用戶已經登錄(先前已通過身份驗證和授權),該函數將返回並重定向到我放置博客列表的起始頁面。 這對我很好。
我的問題是:當用戶未登錄時,我想打開另一個頁面(“歡迎”頁面),在該頁面上我將獲得有關應用程序和登錄按鈕的一些基本信息。 在這種情況下,當用戶單擊該按鈕時,我只想調用GoogleWebAuthorizationBroker.AuthorizeAsync()函數,該函數會將我重定向到登錄頁面。 但是我找不到方法來檢查用戶是否已經登錄,僅在這種情況下顯示“歡迎”頁面。
這是我使用GoogleWebAuthorizationBroker.AuthorizeAsync()函數的方式。
m_credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "my_client_id",
ClientSecret = "my_client_secret"
},
new[] { BloggerService.Scope.Blogger },
"user", CancellationToken.None);
最終,我找到了問題的解決方案,並希望在這里分享它對某人可能有所幫助。
很棒的Google API代碼向所有人開放,因此我可以深入研究GoogleWebAuthorizationBroker.AuthorizeAsync()函數並查看其執行的所有步驟。 這是我可以收集到一個功能中的所有步驟
private static async Task<UserCredential> AuthorizeAsync()
{
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Constants.ClientId,
ClientSecret = Constants.ClientSecret
},
Scopes = new[] { BloggerService.Scope.Blogger },
DataStore = new StorageDataStore()
};
var flow = new AuthorizationCodeFlow(initializer);
var codeReceiver = new AuthorizationCodeBroker();
var token = await flow.LoadTokenAsync("user", CancellationToken.None).ConfigureAwait(false);
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
{
var redirectUri = codeReceiver.RedirectUri;
AuthorizationCodeRequestUrl codeRequest = flow.CreateAuthorizationCodeRequest(redirectUri);
var response = await codeReceiver.ReceiveCodeAsync(codeRequest, CancellationToken.None).ConfigureAwait(false);
if (string.IsNullOrEmpty(response.Code))
{
var errorResponse = new TokenErrorResponse(response);
throw new TokenResponseException(errorResponse);
}
token = await flow.ExchangeCodeForTokenAsync("user", response.Code, codeReceiver.RedirectUri,
CancellationToken.None).ConfigureAwait(false);
}
m_credential = new UserCredential(flow, "user", token);
return m_credential;
}
從GoogleWebAuthorizationBroker.AuthorizeAsync()函數依次調用的不同函數收集的步驟。 從代碼中我們可以看到這一行
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
它檢查用戶是否已經通過身份驗證。 如果是,則進行下一步,否則將對用戶進行身份驗證。 所以我可以為我編寫這個簡單的函數:
public async Task<bool> IsUserAuthenticated()
{
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Constants.ClientId,
ClientSecret = Constants.ClientSecret
},
Scopes = new[] { BloggerService.Scope.Blogger },
DataStore = new StorageDataStore()
};
var flow = new AuthorizationCodeFlow(initializer);
var codeReceiver = new AuthorizationCodeBroker();
var token = await flow.LoadTokenAsync("user", CancellationToken.None).ConfigureAwait(false);
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
{ return false; }
else
{ return true; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.