C# Google OAuth2 检查身份验证状态

huangapple go评论60阅读模式
英文:

C# google oauth2 check authentication state

问题

有没有办法检查用户是否已经登录?

this.userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
  clientSecrets.Secrets,
  new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
  Globals.GetGDUserName(),
  CancellationToken.None
);

我正在使用上面的代码来授权用户,但如果没有用户已经登录,它会自动提示用户选择帐户。

所以,有没有办法检查用户是否已经登录。

如果我将这段代码放在初始化函数中,即使用户不想登录,它也可能提示用户,这就是我正在寻找一种检查用户是否已经登录的方法。

我找到了类似prompt = none的东西,但我该如何在上面的代码中或以其他方式使用它?

英文:

Is there any way to check if the user is already logged in or not?

this.userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
  clientSecrets.Secrets,
  new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
  Globals.GetGDUserName(),
  CancellationToken.None
);

I am using the above code to authorize the user, but it automatically prompts the user to select account if no one is already logged in.

So, is there any way to check if the user is already logged in.

If I put this code in initialization function, it might prompt the user even if he doesn't want to, that's why I am looking for the way to check if the user is already logged in.

I did find something like prompt = none, but how can I use that in the code above or in any other way?

答案1

得分: 1

GoogleAuthorizationCodeFlow.Initializer对象中,您可以将prompt参数设置为"none"并自定义流程,以指示如果用户尚未登录,则不要提示用户进行授权。此外,您可以使用令牌值来检查用户之前是否已登录。无论是否存在令牌,您都可以执行相关的交易。

var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = clientSecrets.Secrets,
    Scopes = new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
    DataStore = // 指定数据存储,
    Prompt = "none"
};

var flow = new GoogleAuthorizationCodeFlow(initializer);

var token = await flow.LoadTokenAsync(userId);

if (token == null)
{
    // 用户未登录 - 您的代码
    this.userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        clientSecrets.Secrets,
        new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
        Globals.GetGDUserName(),
        CancellationToken.None
    );
}
else
{
    // 用户已经登录
    this.userCredential = new UserCredential(flow, userId, token);
}

数据存储
用户凭据

英文:

In the GoogleAuthorizationCodeFlow.Initializer object, you can set the prompt parameter to "none" and customize the process to indicate that you do not want to prompt the user for authorization if they are not yet logged in. In addition; You can use the token value to check that the user has been before. If there is a token or not, you can perform the relevant transactions.

var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = clientSecrets.Secrets,
    Scopes = new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
    DataStore = // Specify data store,
    Prompt = "none"
};

var flow = new GoogleAuthorizationCodeFlow(initializer);

var token = await flow.LoadTokenAsync(userId);

if (token == null)
{
    // User is not logged in - your code
    this.userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        clientSecrets.Secrets,
        new[] { DriveService.Scope.DriveFile, emailScope, profileScope },
        Globals.GetGDUserName(),
        CancellationToken.None
    );
}
else
{
    // User is already logged in
    this.userCredential = new UserCredential(flow, userId, token);
}

Data Store
User credentials

huangapple
  • 本文由 发表于 2023年4月4日 15:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定