在Register.aspx.cs中注册时不发送确认电子邮件

时间:2017-08-30 15:48:37

标签: c# asp.net authentication asp.net-identity

根据项目的Visual Studio 2017帐户文件夹中的注册代码中的说明,使用来自Microsoft身份验证和密码恢复的说明(C#)。

错误讯息:

  

System.InvalidOperationException:'找不到UserId。'

失败的地方: 在 Register.aspx.cs

using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using FHSF_5_DEV.Models;
        :
        :
        :

        protected void CreateUser_Click(object sender, EventArgs e)
        {
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
            var user = new ApplicationUser() { UserName = UserName.Text, Email = UserEmail.Text };
            IdentityResult result = manager.Create(user, Password.Text);
            if (result.Succeeded)
            {
                string code = manager.GenerateEmailConfirmationToken(user.Id);
                string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Email, Request);
         ==>    manager.SendEmail(user.Email, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");**

1 个答案:

答案 0 :(得分:0)

使用User.Email代替User.ID.原始代码设置了身份验证代码,假设电子邮件和用户ID /用户名相同。