c# - 没有注册 IUserTokenProvider

标签 c# asp.net asp.net-identity-2

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

有一些我想尝试的新功能,比如 GenerateEmailConfirmationToken

我正在使用 this项目作为引用。

当用户尝试注册时,我在执行 Register 的 Post 方法时出错

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);
}

行内

 var code = _userManager.GenerateEmailConfirmationToken(user.Id);

我收到错误提示:

No IUserTokenProvider is registered.

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

或者是否需要更改某些内容才能使这些功能正常工作?

最佳答案

您必须指定 UserTokenProvider 才能生成 token 。

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"));

您还应该阅读这篇文章:Adding Two Factor Authentication to an Application Using ASP.NET Identity .

关于c# - 没有注册 IUserTokenProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629936/

相关文章:

c# - Identity 2.0 无效登录尝试

asp.net - .Net 身份 2 与 3

c# - 将 ASP.net Identity 2.0 连接到现有数据库

c# - 使用 wsDualHttpBinding 通过 Internet 连接到 WCF 服务超时

c# - asp.net 中的替代图像显示

c# - IReliable ReadWrite DocumentClient 在 Documentdb 中缺少分区解析器

c# - 如何将 span 元素放入 ActionLink MVC3 中?

c# - 使用 asp.net c# 将图像从文件夹显示到页面

c# - 如果列表中包含后缀,则使用 LINQ 从字符串中删除后缀?

c# - 使用MVVM,如何使用App.config动态生成 View