如何在Azure Web作业中实例化OWIN IDataProtectionProvider? [英] How can I instantiate OWIN IDataProtectionProvider in Azure Web Jobs?

查看:49
本文介绍了如何在Azure Web作业中实例化OWIN IDataProtectionProvider?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个IDataProtectionProvider实例,以在Azure Web Jobs工作程序中使用Identity Framework UserManager生成电子邮件确认令牌:

I need an instance of IDataProtectionProvider to generate email confirmation tokens using the Identity Framework UserManager in an Azure Web Jobs worker:

var confirmToken = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

崩溃是因为在构造时将空IUserTokenProvider<User, int>传递给了UserManager<User, int>.

This crashes because a null IUserTokenProvider<User, int> was passed to the UserManager<User, int> upon constuction.

在MVC应用程序中,实例是这样创建的:

In the MVC application an instance is created like this:

public class OWINStartup
{
    public void Configuration(IAppBuilder app)
    {
        var dataProtectionProvider = app.GetDataProtectionProvider();

但是,当然,Azure Web Jobs没有OWINStartup挂钩.有什么建议吗?

But of course, Azure Web Jobs doesn't have an OWINStartup hook. Any advice?

推荐答案

查看 Katana 源代码,用于 OWIN启动上下文,您可以看到DataProtectionProvider的默认实现是MachineKeyDataProtectionProvider.不幸的是,该类没有公开,只有托管在天蓝色的情况下时将不起作用.

Taking a look at the Katana source code for the OWIN startup context you can see the default implementation of the DataProtectionProvider is a MachineKeyDataProtectionProvider. Unfortunately this class is not exposed to us, only the DpapiDataProtectionProvider which will not work when hosted in azure.

您可以在此处找到MachineKeyDataProtectionProvider的实现 .您还需要实现自己的MachineKeyDataProtector,如此处所示.这些都不是困难的实现,实际上是 MachineKey.Protect() MachineKey.Unprotect() .

You can find the implementation of the MachineKeyDataProtectionProvider here. You will need to also implement your own MachineKeyDataProtector as seen here. These are not difficult implmentations and are essentially wrappers around MachineKey.Protect() and MachineKey.Unprotect().

Katana 项目源(一旦实现,就很容易插入UserManager:

Once you have that implemented it is easy to plug into the UserManager:

var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
var machineKeyProtectionProvider = new MachineKeyProtectionProvider();
usermanager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(machineKeyProtectionProvider.Create("ASP.NET Identity"));

希望能帮助您朝正确的方向前进.

Hope that helps get you in the right direction.

这篇关于如何在Azure Web作业中实例化OWIN IDataProtectionProvider?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆