gpt4 book ai didi

asp.net-mvc - 在 MVC 5 中正确获取 DataProtectionProvider 以进行依赖项注入(inject)

转载 作者:行者123 更新时间:2023-12-03 03:28:22 26 4
gpt4 key购买 nike

当尝试手动创建 DataProtectionProvider 时,我偶然发现了 Microsoft 文档 DpapiDataProtectionProvider其中说:

Used to provide the data protection services that are derived from the Data Protection API. It is the best choice of data protection when you application is not hosted by ASP.NET and all processes are running as the same domain identity.

突然出现一个问题:当您的应用程序由 ASP.NET 托管时,最佳选择是什么?

进一步搜索,似乎最好的选择是从OWIN获取DataProtectionProvider。这可以在启动配置中完成,其中您有 IAppBuilder 并使用位于 Microsoft.Owin.Security.DataProtection 命名空间中的 AppBuilderExtensions,您可以调用 app.GetDataProtectionProvider()

到目前为止,我还是很满意的。但是,现在您想要将 DataProtectionProvider 注入(inject)到类的构造函数中(例如 UserManager)。我见过one suggestionDataProtectionProvider 存储在静态属性中,然后在需要的地方使用它,但这似乎是一个相当错误的解决方案。

我认为类似于以下代码段的解决方案是合适的(使用 ninject 容器):

kernel.Bind<IDataProtectionProvider>()
// beware, method .GetDataProtectionProvider() is fictional
.ToMethod(c => HttpContext.Current.GetOwinContext().GetDataProtectionProvider())
.InRequestScope();

最佳答案

有一个walkthrough它告诉您如何向 Autofac 注册 DataProtectionProvider。

builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerRequest();

关于asp.net-mvc - 在 MVC 5 中正确获取 DataProtectionProvider 以进行依赖项注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36473209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com