没有注册 IUserTokenProvider [英] No IUserTokenProvider is registered

查看:26
本文介绍了没有注册 IUserTokenProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently updated Asp.Net Identity Core of my application form 1.0 to 2.0.

我想尝试一些新功能,例如 GenerateEmailConfirmationToken

There are new features which I wanted to try like GenerateEmailConfirmationToken, etc.

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

I'm using this project as a reference.

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

When the user tries to register, I get error during the execution of Post method of 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);
}

在线

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

我收到错误提示:

No IUserTokenProvider is registered.

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

For now, I just wanted to see what kind of code it generates. Is there some change I need to make to my ApplicationUser class that inherits from IdentityUser class?

或者我需要更改什么才能使这些功能正常工作?

Or is there something I need to change to get those function work?

推荐答案

你必须指定一个 UserTokenProvider 来生成一个令牌.

You have to specify a UserTokenProvider to generate a 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"));

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

You should also read this article: Adding Two Factor Authentication to an Application Using ASP.NET Identity.

这篇关于没有注册 IUserTokenProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆