如何在asp.net中生成刷新令牌?

时间:2019-05-30 05:08:26

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

我可以使用Google文件选择器从客户端获取访问令牌。但是访问令牌将在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
                });

我尝试过这个,但是之后我不知道如何前进。

1 个答案:

答案 0 :(得分:0)

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

您可以尝试以下代码段:

刷新令牌示例:

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

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

相关问题