如何在新的ASP.NET身份系统中获取用户的密码?或者如何在不知道当前密码(用户忘记密码)的情况下重置?
我正在使用UserManagerASP.NET 5附带的内置类在我的站点上实现重置密码功能.
在我的开发环境中一切正常.但是,一旦我在作为Azure网站运行的生产站点中尝试它,我会得到以下异常:
System.Security.Cryptography.CryptographicException:数据保护操作失败.这可能是由于没有为当前线程的用户上下文加载用户配置文件引起的,这可能是线程模拟时的情况.
这是我设置UserManager实例的方式:
var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider(SiteConfig.SiteName);
UserManager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<User>(provider.Create(ResetPasswordPurpose));
Run Code Online (Sandbox Code Playgroud)
然后,我生成令牌(通过电子邮件发送给用户,以便他们可以验证他们确实想要重置密码):
string token = UserManager.GeneratePasswordResetToken(user.Id);
Run Code Online (Sandbox Code Playgroud)
不幸的是,当它在Azure上运行时,我得到了上面的例外.
我用Google搜索并发现了这种可能的解决方案.但是,它根本不起作用,我仍然得到同样的例外.
根据该链接,它与会话令牌有关,这些会话令牌不在像Azure这样的Web场上工作.
我正在使用Unity for Dependencies Injection并使用Identiy Provider来管理用户登录,注册,电子邮件确认等.
当我尝试注册用户时,我遇到了这个问题:
当前类型Microsoft.Owin.Security.IAuthenticationManager是一个接口,无法构造.你错过了类型映射吗?
我不知道如何在我的Unity容器中注册此接口(IAuthenticationManager).
我尝试使用此代码注册接口,但如果我把它,我还有其他问题:
没有注册IUserTokenProvider.
container.RegisterType<HttpContextBase>(
new InjectionFactory(_ => new HttpContextWrapper(HttpContext.Current)));
container.RegisterType<IOwinContext>(new InjectionFactory(c => c.Resolve<HttpContextBase>().GetOwinContext()));
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(c => c.Resolve<IOwinContext>().Authentication));
Run Code Online (Sandbox Code Playgroud)
我把一些应用程序的代码(如果我不使用Unity,一切正常):
的AccountController
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
Run Code Online (Sandbox Code Playgroud)
IdentityConfig.cs
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用asp.net标识并使用structuremap作为DI框架.问题是当我使用构造函数注入时,ApplicationUserManager没有配置它的所有成员,例如TokenProvider,...
这是我的ApplicationUserManager类:
public class ApplicationUserManager : UserManager<User, long>
{
public ApplicationUserManager(IUserStore<User, long> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<InsuranceManagementContext>()));
// Configure the application user manager
manager.UserValidator = new UserValidator<User, long>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
};
manager.PasswordValidator = new PasswordValidator
{
RequireDigit = true,
RequiredLength = 8,
RequireLowercase = false,
RequireNonLetterOrDigit = true,
RequireUppercase = false
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{ …Run Code Online (Sandbox Code Playgroud) structuremap asp.net-mvc dependency-injection asp.net-identity asp.net-identity-2
我的GeneratePasswordResetTokenAsync()方法被调用时出错了.我用owin身份配置了autofac.
错误是:
使用依赖项注入时,未注册IUserTokenProvider
在我的sample.web项目有一个AutofacConfig.cs地方我寄存器文件signinmanager和usermanager我在创建sample.repository项目.
AutofacConfig.cs
public class AutofacConfig
{
public static Autofac.IContainer RegisterDependencies()
{
var containerBuilder = new ContainerBuilder();
// REGISTER DEPENDENCIES
containerBuilder.RegisterType<SampleDataContext>()
.As<DbContext>()
.InstancePerDependency();
containerBuilder.RegisterType<UserStore<SampleUser>>()
.As<IUserStore<SampleUser>>()
.InstancePerRequest();
containerBuilder.RegisterType<ApplicationUserManager>()
.AsSelf()
.InstancePerRequest();
containerBuilder.RegisterType<ApplicationSignInManager>()
.AsSelf()
.InstancePerRequest();
containerBuilder.RegisterType<EmailService>();
containerBuilder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication)
.InstancePerRequest();
var container = containerBuilder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
return container;
}
}
Run Code Online (Sandbox Code Playgroud)
ApplicationUserManager.cs
public class ApplicationUserManager : UserManager<SampleUser>
{
public ApplicationUserManager(IUserStore<SampleUser> store)
: …Run Code Online (Sandbox Code Playgroud)