C# 如何在 ASP.NET Identity 1.1 每晚构建中实现 TokenProvider?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19539579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a TokenProvider in ASP.NET Identity 1.1 nightly build?
提问by graycrow
I'm trying to implement password reset functionality with nightly build of ASP.NET Identity 1.1. There is a UserManager.GetPasswordResetToken method, but it throws an exception "No ITokenProvider is registered". Is there an built in token provider in ASP.NET Identity? If yes, how I can register it? If no, how I can implement one? Will be default Token Provider in the 1.1. release? And final question, is there an estimated 1.1 release date?
我正在尝试使用 ASP.NET Identity 1.1 的每晚构建来实现密码重置功能。有一个 UserManager.GetPasswordResetToken 方法,但它抛出异常“没有注册 ITokenProvider”。ASP.NET Identity 中是否有内置令牌提供程序?如果是,我该如何注册?如果没有,我该如何实施?将是 1.1 中的默认令牌提供程序。释放?最后一个问题,是否有估计的 1.1 发布日期?
采纳答案by Hao Kung
The default token provider implementation is found in the Microsoft.Identity.Owin
package:
默认令牌提供程序实现可在Microsoft.Identity.Owin
包中找到:
/// <summary>
/// Token provider that uses a DataProtector to generate encrypted tokens
/// </summary>
public class DataProtectorTokenProvider : ITokenProvider {
public DataProtectorTokenProvider(IDataProtector protector)
And you do something like this to wire one up using the default data protection provider from your OWIN IAppBuilder
您可以使用 OWIN 中的默认数据保护提供程序执行类似操作 IAppBuilder
IDataProtectionProvider provider = app.GetDataProtectionProvider();
if (provider != null)
{
manager.PasswordResetTokens = new DataProtectorTokenProvider(provider.Create("PasswordReset"));
manager.UserConfirmationTokens = new DataProtectorTokenProvider(provider.Create("ConfirmUser"));
}
回答by graycrow
Ok, answering my own question based on @hao-kung reply. First add static constructor and UserManagerFactory to Statrup class (startup.auth.cs)
好的,根据@hao-kung 的回复回答我自己的问题。首先在Statrup类(startup.auth.cs)中添加静态构造函数和UserManagerFactory
public partial class Startup
{
static Startup()
{
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
}
public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; }
public void ConfigureAuth(IAppBuilder app)
{
var manager = UserManagerFactory();
IDataProtectionProvider provider = app.GetDataProtectionProvider();
if (provider != null)
{
manager.PasswordResetTokens = new DataProtectorTokenProvider(provider.Create("PasswordReset"));
manager.UserConfirmationTokens = new DataProtectorTokenProvider(provider.Create("ConfirmUser"));
}
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
Then init UserManager in the AccountController using that UserManagerFactory
然后使用 UserManagerFactory 在 AccountController 中初始化 UserManager
public AccountController() : this(Startup.UserManagerFactory())
{
}
public AccountController(UserManager<IdentityUser> userManager)
{
UserManager = userManager;
}
public UserManager<IdentityUser> UserManager { get; private set; }
回答by Scott Dorman
Another way to do this (building on the other answers but simplifying it some) is to change Startup.Auth.cs
so it looks similar to this:
另一种方法(建立在其他答案的基础上,但对其进行了一些简化)是进行更改Startup.Auth.cs
,使其看起来类似于:
public partial class Startup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void ConfigureAuth(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
}
}
Then, modify the default constructor in AccountController.cs
so that it looks similar to this:
然后,修改默认构造函数AccountController.cs
,使其看起来类似于:
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
if (Startup.DataProtectionProvider != null)
{
this.UserManager.PasswordResetTokens = new DataProtectorTokenProvider(Startup.DataProtectionProvider.Create("PasswordReset"));
this.UserManager.UserConfirmationTokens = new DataProtectorTokenProvider(Startup.DataProtectionProvider.Create("ConfirmUser"));
}
}
回答by Vikash Kumar
If someone looking for solution under AspNet.Identity 2.0 beta1 version.
如果有人在 AspNet.Identity 2.0 beta1 版本下寻找解决方案。
Only this need to be modified.
只有这个需要修改。
UserManager.UserTokenProvider = new DataProtectorTokenProvider
<SecurityUser, string>(provider.Create("UserToken"))
as IUserTokenProvider<SecurityUser, string>;
PasswordResetTokens
and UserConfirmationTokens
is merged into UserTokenProvider
property and token provider class is also modified.
PasswordResetTokens
并UserConfirmationTokens
合并到UserTokenProvider
属性和令牌提供程序类也被修改。