【问题标题】:How can I generate refresh token in asp.net?如何在 asp.net 中生成刷新令牌?
【发布时间】:2019-10-15 17:42:38
【问题描述】:

我可以使用谷歌文件选择器从客户端获取访问令牌。但是访问令牌会在 3600 秒后过期。我挠头来获取刷新令牌,但无法在 C# ASP.NET 上执行此操作。有人可以帮助 C# 代码理解和检索刷新令牌吗?这将非常有帮助。谢谢。

我尝试使用 GoogleWebAuthorizationBroker.AuthorizeAsync,它在我的本地机器上工作,但在生产 IIS 服务器上不工作。我也尝试过 GoogleAuthorizationCodeFlow,但无法进一步使用它,我不知道如何使用它。

IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = System.Configuration.ConfigurationManager.AppSettings["GoogleDriveClientID"],
                        ClientSecret = System.Configuration.ConfigurationManager.AppSettings["GoogleDriveClientSecret"]
                    },
                    Scopes = Scopes,
                    DataStore = null
                });

我试过这个,但我不知道之后如何继续。

【问题讨论】:

  • 你能包括你对Scopes的定义吗?
  • 公共静态字符串[] Scopes = { DriveService.Scope.Drive };
  • @Vivek 看看能不能解决你的问题

标签: c# google-drive-api azure-active-directory access-token refresh-token


【解决方案1】:

您似乎正在尝试从您的 C# 代码示例中生成刷新令牌。

你可以试试下面的代码 sn-p:

刷新令牌示例:

public class AppFlowMetadata : FlowMetadata
    {
        private static readonly IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = "yourClientId",
                        ClientSecret = "yourClientSecret"
                    },
                    Scopes = new[] { DriveService.Scope.Drive },
                    DataStore = new FileDataStore("Drive.Api.Auth.Store")
                });

        public override string GetUserId(Controller controller)
        {
            // In this sample we use the session to store the user identifiers.
            // That's not the best practice, because you should have a logic to identify
            // a user. You might want to use "OpenID Connect".
            // You can read more about the protocol in the following link:
            // https://developers.google.com/accounts/docs/OAuth2Login.
            var user = controller.Session["user"];
            if (user == null)
            {
                user = Guid.NewGuid();
                controller.Session["user"] = user;
            }
            return user.ToString();

        }

        public override IAuthorizationCodeFlow Flow
        {
            get { return flow; }
        }
    }

注意:如果您有任何疑问,您将如何获得ClientIdClientSecretScope 请参考此docs MVC Example

如果您仍有任何疑问,请随时分享。谢谢,编码愉快!

【讨论】:

  • 谢谢。但我使用的是 ASP.NET,所以我认为我必须相应地修改这段代码,因为它是为 MVC 编写的。
  • No Problem 功能相同。 asp.net web forms和MVC没有区别
猜你喜欢
  • 2019-10-01
  • 2018-05-12
  • 2019-05-25
  • 2020-09-21
  • 2020-04-14
  • 2019-03-08
  • 2021-05-15
  • 2018-12-08
  • 1970-01-01
相关资源
最近更新 更多