106

最近Asp.Net Identity Core、申請フォームを 1.0 から 2.0 に更新しました。

GenerateEmailConfirmationTokenなど、試してみたかった新機能があります。

このプロジェクトを参考にしています。

ユーザーが登録しようとすると、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.

とりあえず、生成されるコードの種類を確認したかっただけです。ApplicationUserクラスから継承するクラスに変更を加える必要はありIdentityUserますか?

または、これらの機能を機能させるために変更する必要があるものはありますか?

4

11 に答える 11

131

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

この記事もお読みください: ASP.NET ID を使用してアプリケーションに 2 要素認証を追加する

于 2014-03-25T13:03:33.353 に答える
22

受け入れられた回答に加えて、このアプローチは Azure Web サイトでは機能しないことを追加したいと思います。トークンの代わりに CryptographicException が発生します。

Azure 向けに修正するには、独自の IDataProtector を実装してください:この回答を参照してください。

ブログ投稿でもう少し詳しく

于 2014-06-21T00:19:13.360 に答える
15

私の解決策:

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

このコードに関する私の問題は解決しました。
頑張ってください。

于 2014-11-12T01:42:16.140 に答える
9

他の誰かが私と同じ間違いをした場合に備えて。以下のような関数を作成しようとしましたが、「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();
     }

機能しない最初の試行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();
     }
于 2015-12-29T16:59:11.190 に答える
1

Identity を更新した後も同じエラーが発生し、Unity を使用していたことが原因であることがわかりました。

解決策については、この質問スレッドを参照してください: IAuthenticationManager を Unity に登録する

クイック リファレンスとして以下も参照してください。

Unity を ASP.NET Identity 2.0 とうまく連携させるために私が行ったことは次のとおりです。

クラスのRegisterTypesメソッドに次を追加しました。UnityConfig

container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<AccountController>(new InjectionConstructor());
于 2015-02-03T17:32:09.037 に答える