代码之家  ›  专栏  ›  技术社区  ›  justindao

Azure广告,ASP。NET核心Web应用程序和服务结构:如何让auth在多台机器上工作?

  •  1
  • justindao  · 技术社区  · 9 年前

    经过一些挖掘,我发现默认情况下,机器密钥用于加密cookie。我的SF实例有多台机器在运行,这似乎是罪魁祸首(不同的机器无法读取同一个cookie,因此,认为我没有经过身份验证)。

    有没有办法让AAD cookie验证在机器之间使用相同的密钥?

    ConfigureServices

    services.AddDataProtection()
        .SetApplicationName("my-app")
        .ProtectKeysWithCertificate("my-thumbprint")
        .PersistKeysToFileSystem(new DirectoryInfo("dp-keys"));
    

    但这似乎仍然不起作用-每台机器上仍会生成不同的密钥。目前,我没有办法在所有机器之间共享文件-这是唯一的解决方案,还是还有其他解决方案?

    2 回复  |  直到 9 年前
        1
  •  2
  •   Poul K. Sørensen    9 年前

    由于我们在服务结构上,您已经正确部署了一个证书,您的集群可以使用它。否则,您必须添加证书。

    var cert = X509.LocalMachine.My.Thumbprint.Find("C03BB5A6410741CDD2927B4FF88C3E67215A393B", validOnly: false).FirstOrDefault();
    services.AddApplicationStorageDataProtection(_container, cert);
    

        public static IServiceCollection AddApplicationStorageDataProtection(this IServiceCollection services, IUnityContainer container, X509Certificate2 cert )
        {
            if (container != null)
            {
    
                try
                {
                    var storage = container.Resolve<IApplicationStorageService>();
                    var token = storage.GetApplicationStorageSharedAccessSignature().GetAwaiter().GetResult();
                    var name = storage.GetApplicationStorageAccountNameAsync().GetAwaiter().GetResult();
                    var a = new CloudStorageAccount(new StorageCredentials(token), name, null, true);
                    var c = a.CreateCloudBlobClient().GetContainerReference("dataprotection");
                    c.CreateIfNotExists();
    
                    services.AddDataProtection()
                     .ProtectKeysWithCertificate(cert)
                     .PersistKeysToAzureBlobStorage(c.GetBlockBlobReference("dummy.csrf"));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw;
                }
            }
    
            return services;
        }
    

    当然,您必须根据需要更改代码。这里提供的代码使用集群中部署的存储服务,因此我不需要管理所有应用程序中存储的凭据,只需向存储服务请求凭据,而且unity容器就在那里,因为这是我正在使用的底层DI框架。

     services.AddDataProtection()
                     .ProtectKeysWithCertificate(cert)
                     .PersistKeysToAzureBlobStorage(c.GetBlockBlobReference("dummy.csrf"));
    
        2
  •  0
  •   LoekD    9 年前

    在web场场景中,您需要跨服务器使用共享机密来保护cookie。这 P&P link

    默认情况下,当您使用Windows Identity Foundation(WIF)时 框架来管理您的身份基础设施,它加密 通过使用 对于具有多个角色的应用程序来说,不是可行的解决方案 实例,因为每个角色实例将使用不同的加密 例子您必须使用加密机制,例如RSA,它使用

    通过使用证书进行加密解决了这个问题。

    new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate)