如何在 Azure Web 作业中实例化 OWIN IDataProtectionProvider?
How can I instantiate OWIN IDataProtectionProvider in Azure Web Jobs?
我需要 IDataProtectionProvider
的一个实例,以便在 Azure Web 作业工作人员中使用身份框架 UserManager
生成电子邮件确认令牌:
var confirmToken = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
此崩溃是因为在构建时将 null IUserTokenProvider<User, int>
传递给了 UserManager<User, int>
。
在 MVC 应用程序中,实例是这样创建的:
public class OWINStartup
{
public void Configuration(IAppBuilder app)
{
var dataProtectionProvider = app.GetDataProtectionProvider();
当然,Azure Web Jobs 没有 OWINStartup
挂钩。有什么建议吗?
正在查看 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
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 project source (apache 2.0 license 的 MachineKeyDataProtectionProvider
和 MachineKeyDataProtector
的实施):
internal class MachineKeyProtectionProvider : IDataProtectionProvider
{
public IDataProtector Create(params string[] purposes)
{
return new MachineKeyDataProtector(purposes);
}
}
internal class MachineKeyDataProtector : IDataProtector
{
private readonly string[] _purposes;
public MachineKeyDataProtector(string[] purposes)
{
_purposes = purposes;
}
public byte[] Protect(byte[] userData)
{
return MachineKey.Protect(userData, _purposes);
}
public byte[] Unprotect(byte[] protectedData)
{
return MachineKey.Unprotect(protectedData, _purposes);
}
}
一旦你实现了,就很容易插入 UserManager
:
var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
var machineKeyProtectionProvider = new MachineKeyProtectionProvider();
usermanager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(machineKeyProtectionProvider.Create("ASP.NET Identity"));
希望能帮助您朝着正确的方向前进。
我需要 IDataProtectionProvider
的一个实例,以便在 Azure Web 作业工作人员中使用身份框架 UserManager
生成电子邮件确认令牌:
var confirmToken = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
此崩溃是因为在构建时将 null IUserTokenProvider<User, int>
传递给了 UserManager<User, int>
。
在 MVC 应用程序中,实例是这样创建的:
public class OWINStartup
{
public void Configuration(IAppBuilder app)
{
var dataProtectionProvider = app.GetDataProtectionProvider();
当然,Azure Web Jobs 没有 OWINStartup
挂钩。有什么建议吗?
正在查看 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
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 project source (apache 2.0 license 的 MachineKeyDataProtectionProvider
和 MachineKeyDataProtector
的实施):
internal class MachineKeyProtectionProvider : IDataProtectionProvider
{
public IDataProtector Create(params string[] purposes)
{
return new MachineKeyDataProtector(purposes);
}
}
internal class MachineKeyDataProtector : IDataProtector
{
private readonly string[] _purposes;
public MachineKeyDataProtector(string[] purposes)
{
_purposes = purposes;
}
public byte[] Protect(byte[] userData)
{
return MachineKey.Protect(userData, _purposes);
}
public byte[] Unprotect(byte[] protectedData)
{
return MachineKey.Unprotect(protectedData, _purposes);
}
}
一旦你实现了,就很容易插入 UserManager
:
var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
var machineKeyProtectionProvider = new MachineKeyProtectionProvider();
usermanager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(machineKeyProtectionProvider.Create("ASP.NET Identity"));
希望能帮助您朝着正确的方向前进。