没有注册IUserTokenProvider

Cyb*_*cop 98 c# asp.net asp.net-identity-2

我最近更新Asp.Net Identity Core了我的申请表1.0到2.0.

有一些我想尝试的新功能GenerateEmailConfirmationToken,等等.

我正在使用这个项目作为参考.

当用户尝试注册时,我在执行Register方法时遇到错误

private readonly UserManager<ApplicationUser> _userManager;     

public ActionResult Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var ifUserEXists = _userManager.FindByName(model.EmailId);
        if (ifUserEXists == null) return View(model);
        var confirmationToken = _userRepository.CreateConfirmationToken();//this is how i'm generating token currently.                
        var result = _userRepository.CreateUser(model,confirmationToken);
        var user = _userManager.FindByName(model.EmailId);
        if (result)
        {
            var code = _userManager.GenerateEmailConfirmationToken(user.Id);//error here
            _userRepository.SendEmailConfirmation(model.EmailId, model.FirstName, confirmationToken);
            //Information("An email is sent to your email address. Please follow the link to activate your account.");
            return View("~/Views/Account/Thank_You_For_Registering.cshtml");
        }     
    }

    //Error("Sorry! email address already exists. Please try again with different email id.");
    ModelState.AddModelError(string.Empty, Resource.AccountController_Register_Sorry__User_already_exists__please_try_again_);
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

在线

 var code = _userManager.GenerateEmailConfirmationToken(user.Id);
Run Code Online (Sandbox Code Playgroud)

我得到错误说:

No IUserTokenProvider is registered.
Run Code Online (Sandbox Code Playgroud)

现在,我只是想看看它生成了什么样的代码.我需要对ApplicationUser继承自IdentityUser类的类进行一些更改吗?

或者有什么我需要改变以使这些功能工作?

mez*_*tou 123

您必须指定a UserTokenProvider才能生成令牌.

using Microsoft.Owin.Security.DataProtection;
using Microsoft.AspNet.Identity.Owin;
// ...

var provider = new DpapiDataProtectionProvider("SampleAppName");

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
    provider.Create("SampleTokenName"));
Run Code Online (Sandbox Code Playgroud)

您还应该阅读本文:使用ASP.NET标识向应用程序添加双因素身份验证.

  • 什么是"Sample"和"EmailConfirmation"?一些文字可以是什么? (5认同)
  • 是的,但您必须使用相同的方法来生成和验证令牌 (5认同)
  • "Sample"= AppName,"EmailConfirmation"=目的(如参数名称) (2认同)

pis*_*ker 22

在ASP.NET Core中,现在可以在Startup.cs中配置默认​​服务,如下所示:

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

没有必要打电话DpapiDataProtectionProvider或类似的东西.DefaultTokenProviders()将负责从UserManager调用GenerateEmailConfirmationToken.


tra*_*max 21

除了接受的答案之外,我还想补充一点,这种方法在Azure网站中不起作用,你会得到CryptographicException而不是令牌.

要为Azure修复它,请实现您自己的IDataProtector:请参阅此答案

博客文章中稍微详细一些


Ami*_*eri 12

我的解决方案

    var provider = new DpapiDataProtectionProvider("YourAppName");
    UserManager.UserTokenProvider = new DataProtectorTokenProvider<User, string>(provider.Create("UserToken")) 
        as IUserTokenProvider<User, string>;
Run Code Online (Sandbox Code Playgroud)

我解决了这个代码的问题.
祝你好运的朋友.


Ogg*_*las 7

万一其他人犯了同样的错误.我尝试创建一个类似下面的函数,并确定错误"没有注册IUserTokenProvider".走了.但是,当我尝试使用"callbackUrl"生成的链接时,我收到错误"无效令牌".为了使它工作,您需要获取HttpContext UserManager.如果您使用具有单独用户帐户的标准ASP.NET MVC 5应用程序,则可以执行以下操作.

有效的代码:

public ActionResult Index()
     {
         //Code to create ResetPassword URL
         var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
         var user = userManager.FindByName("useremail@gmail.com");
         string code = userManager.GeneratePasswordResetToken(user.Id);
         var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
         return View();
     }
Run Code Online (Sandbox Code Playgroud)

首先尝试不起作用,No IUserTokenProvider is registered.但已经消失,但创建的URL获取Invalid token..

public ActionResult NotWorkingCode()
     {
             //DOES NOT WORK - When used the error "Invalid token." will always trigger.
             var provider = new DpapiDataProtectionProvider("ApplicationName");
             var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
             userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("ASP.NET Identity"));
             var user = userManager.FindByName("useremail@gmail.com");
             string code = userManager.GeneratePasswordResetToken(user.Id);
             var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
             //DOES NOT WORK - When used the error "Invalid token." will always trigger.
         return View();
     }
Run Code Online (Sandbox Code Playgroud)


小智 6

在修改.NET Core的Identity文件时,我偶然发现了这个错误:

NotSupportedException:未注册名为“Default”的 IUserTwoFactorTokenProvider。

Microsoft.AspNetCore.Identity.UserManager.GenerateUserTokenAsync

函数无法生成令牌,因为注册新用户时没有可用的提供程序。不过这个错误很容易修复!

如果您像我一样,则将AddDefaultIdentity文件更改Startup.csAddIdentity. 我想实现我自己的从基本用户继承的用户。通过这样做,我失去了默认的令牌提供者。修复方法是将它们添加回来AddDefaultTokenProviders()

        services.AddIdentity<User, UserRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

修复后,一切又恢复正常了!

来源:https ://mattferderer.com/NotSupportedException-No-IUserTwoFactorTokenProvider-tuser-named-default-registered


Sco*_*ttC 5

按照上面的pisker

在startup.cs中你可以使用

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

在.net core 2.0中,您可能需要添加到startup.cs:

using Microsoft.AspNetCore.Identity;
Run Code Online (Sandbox Code Playgroud)

这对我来说很好.

  • 我认为记住这是一个 Asp .NET Core 解决方案很重要,它不适用于 Asp .NET Framework。 (2认同)