首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未注册任何IUserTokenProvider

未注册任何IUserTokenProvider
EN

Stack Overflow用户
提问于 2014-03-25 17:14:18
回答 8查看 49K关注 0票数 106

我最近将我的应用程序的Asp.Net Identity Core从1.0更新到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类进行一些更改吗?

或者我需要做些改变才能让这些函数正常工作?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-03-25 21:03:33

您必须指定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"));

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

票数 131
EN

Stack Overflow用户

发布于 2017-02-23 23:33:53

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

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddDefaultTokenProviders();

不需要调用DpapiDataProtectionProvider或类似的东西。DefaultTokenProviders()将负责从UserManager调用GenerateEmailConfirmationToken。

票数 31
EN

Stack Overflow用户

发布于 2014-06-21 08:19:13

除了公认的答案之外,我还想补充说,这种方法在Azure网站上是行不通的,你会得到CryptographicException而不是token。

要修复Azure,请实现您自己的IDataProtector:see this answer

blog-post中的更多细节

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22629936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档