c# - 如何安全地存储 google drive api 提供的每个用户的 OAuth 响应文件

标签 c# asp.net google-api google-drive-api google-api-dotnet-client

我正在开发一个 asp.net 应用程序,我要求人们授权(使用 OAuth)访问他们的谷歌驱动器(到特定文件夹)以便能够列出其中的文件应用程序。

以下代码使用户能够提供授权并在服务器中创建相应的 Google.Apis.Auth.OAuth2.Responses 文件以供将来的请求使用。但是,每个用户都会发生此请求,这将创建更多的 OAuth 响应文件。我不确定如何设计应用程序并安全地存储这些文件。我可能会为每个用户创建一个新文件夹(使用 Guid-based UserIds)并将文件保存在该文件夹中。这有意义吗?或者您推荐另一种方法?

using (var stream =
    new FileStream("Services/credentials.json", FileMode.Open, FileAccess.Read))
{
    string credPath = "token.json";
    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
    Console.WriteLine("Credential file saved to: " + credPath);
}

BaseClientService.Initializer bcs = new BaseClientService.Initializer();
bcs.HttpClientInitializer = credential;

DriveService service = new DriveService(bcs);

最佳答案

更正

I am working on an asp.net application where I ask people to give the authorization (using OAuth) for accessing their google drive (to a particular folder) to be able to list the files within the application.

当您请求访问权限时,它将针对用户的完整驱动器帐户,而不仅仅是针对一个文件夹。

存储凭证文件。

你的问题有点难以理解,但我认为你是在问你应该在哪里存储你的凭证文件。 FileDataStore 的工作方式是为每个 “用户” 创建一个新文件,因此根据您为“用户”设置的内容,将创建一个文件,如您所见我倾向于在最后使用一个 guid 并将其存储在一个 session 变量中,这样我就知道当用户返回这个 session 变量时我需要为他们获取那个文件。实际上,客户端库会为您完成所有这些工作,因为一旦它看到 id,就会自动使用它。

enter image description here

Web 与安装的应用程序。

如果您正在使用 Asp .net 并打算使用 Web 应用程序,那么我确实有一个主要评论,那么您使用了错误的代码。以下示例显示了如何使用 Web appliations asp.net mvc 进行身份验证请注意下面代码中的 session 。

 public class AppFlowMetadata : FlowMetadata
    {
        private static readonly IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = "PUT_CLIENT_ID_HERE",
                        ClientSecret = "PUT_CLIENT_SECRET_HERE"
                    },
                    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; }
        }
    }

关于c# - 如何安全地存储 google drive api 提供的每个用户的 OAuth 响应文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56201741/

相关文章:

c# - 让一个方法在另一个方法中返回一个字符串是不好的做法吗?

c# - 在哪里可以找到 x64 版本的 Office Interop 程序集?

c# - 如何以小写形式显示网址?

c# - 从代码隐藏 (ASP.NET C#) 到 ASPX 中的图像概览的图像列表

javascript - 使用 Knockout 将数据从数据库加载到 DropDownList

java - 如何知道新文件是否添加到谷歌驱动器中的特定文件夹

c# - 如何在不使用 excel 互操作库的情况下将 excel 工作簿转换为 pdf?

c# - 获取相对路径的绝对路径

android - Google places API,使用邮政编码自动完成

permissions - 启用谷歌电子表格API