检查用户是否已经登录 [英] Check if user is already logged in

查看:72
本文介绍了检查用户是否已经登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows Phone 8.1开发一个应用程序,该应用程序将使用Google Blogger Api.为了获得授权,我正在使用 GoogleWebAuthorizationBroker.AuthorizeAsync()函数,并且一切正常,我可以使用我的博客进行所有操作(获取博客/帖子列表,编辑,删除,添加帖子...).

I am developing an application for Windows Phone 8.1 which will use Google Blogger Api. For authorization I am using the GoogleWebAuthorizationBroker.AuthorizeAsync() function and everything is alright with that, I can do everything with my blog (get the blog/post lists, edit, delete, add posts ...).

在我的应用程序首次打开时,它会调用 GoogleWebAuthorizationBroker.AuthorizeAsync()函数;如果用户已经登录(先前已通过身份验证和授权),则该函数将返回并重定向到我所在的起始页放博客列表.这对我很好.

When my application opens at first it calls GoogleWebAuthorizationBroker.AuthorizeAsync() function and in case when the user is already logged in (previously authenticated and authorized) the function returns and redirects to start page where I put the list of blogs. This is fine for me.

我的问题是:当用户未登录时,我想打开另一个页面(欢迎"页面),在那里我将获得有关我的应用程序和登录按钮的一些基本信息.在这种情况下,当用户单击该按钮时,我只想调用 GoogleWebAuthorizationBroker.AuthorizeAsync()函数,该函数会将我重定向到登录页面.但是我找不到方法来检查用户是否已经登录,仅在这种情况下才显示欢迎"页面.

My problem is: When the user is not logged in I want to open another page ('welcome' page) where I will have some basic info about my application and sign in button. When user clicks that button in that case only I want to call GoogleWebAuthorizationBroker.AuthorizeAsync() function which will redirect me to sign-in page. But I couldn't find method to check if the user is already logged in and only in that case show the 'welcome' page.

这是我使用 GoogleWebAuthorizationBroker.AuthorizeAsync()函数的方式.

Here is how I am using GoogleWebAuthorizationBroker.AuthorizeAsync() function.

m_credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
  new ClientSecrets
  {
    ClientId = "my_client_id",
    ClientSecret = "my_client_secret"
  },
  new[] { BloggerService.Scope.Blogger },
  "user", CancellationToken.None);

推荐答案

最后,我找到了解决问题的方法,并希望在此处分享它对某人可能会有帮助.

Finally I found the solution of my problem and wanted to share it here it can be helpful for someone.

很棒的Google API代码向所有人开放,因此我可以深入研究 GoogleWebAuthorizationBroker.AuthorizeAsync()函数并查看其所做的所有步骤.这是我可以收集到一个功能中的所有步骤

That's wonderful the Google API codes are open for everyone so I could dig inside the GoogleWebAuthorizationBroker.AuthorizeAsync() function and saw all steps it does. Here is the all steps that I could collect into one function

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()函数调用.从代码中我们可以看到这一行

The steps collected from different functions sequentially called by GoogleWebAuthorizationBroker.AuthorizeAsync() function. From code we can see this line

if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))

检查用户是否已通过身份验证.如果是,则进行下一步,否则将对用户进行身份验证.所以我可以为我编写这个简单的函数:

which checks if the user is already authenticated or not. If yes it goes the next step else it authenticates the user. So I could write this simple function for me:

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; }
}

这篇关于检查用户是否已经登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆