使用Identity 2在Web API 2中确认电子邮件的最佳方法是什么?

时间:2015-11-14 02:10:34

标签: asp.net asp.net-web-api asp.net-identity-2

我想使用Visual Studio中的当前模板使用Identity 2构建ASP.net Web API。

我想确认创建帐户的电子邮件地址是有效的电子邮件地址。

我真的找不到如何指导使用当前模板。

我找到了 http://bitoftech.net/2015/02/03/asp-net-identity-2-accounts-confirmation-password-user-policy-configuration/如果从头开始构建,则事情非常混乱。我正在查看他的代码以获取AccountController的注册操作。

string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

await this.AppUserManager.SendEmailAsync(user.Id,"Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

return Created(locationHeader, TheModelFactory.Create(user));

我目前的代码是:

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        // Send an email with this link
        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
        code = HttpUtility.UrlEncode(code);
        var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
        await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
        Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

        return Created(locationHeader, );

他的TheModelFacory.Create我找不到任何地方。我能够找到他为他的项目创建的BaseApiController : ApiController中提到的TheModelFactory,但在我的生活中无法找出适合当前使用个人用户帐户模板的Web Api的位置。

我将T Content而不是TheModelFactory.Create(user)放在哪里才能让它发挥作用?

2 个答案:

答案 0 :(得分:1)

文章中有一个链接带您到GitHub上的源代码

以下是ModelFactory.cs的链接,用于创建模型并复制您想要返回的信息。

public class ModelFactory
{

    private UrlHelper _UrlHelper;
    private ApplicationUserManager _AppUserManager;

    public ModelFactory(HttpRequestMessage request, ApplicationUserManager appUserManager)
    {
        _UrlHelper = new UrlHelper(request);
        _AppUserManager = appUserManager;
    }

    public UserReturnModel Create(ApplicationUser appUser)
    {
        return new UserReturnModel
        {
            Url = _UrlHelper.Link("GetUserById", new { id = appUser.Id }),
            Id = appUser.Id,
            UserName = appUser.UserName,
            FullName = string.Format("{0} {1}", appUser.FirstName, appUser.LastName),
            Email = appUser.Email,
            EmailConfirmed = appUser.EmailConfirmed,
            Level = appUser.Level,
            JoinDate = appUser.JoinDate,
            Roles = _AppUserManager.GetRolesAsync(appUser.Id).Result,
            Claims = _AppUserManager.GetClaimsAsync(appUser.Id).Result
        };

    }

    public RoleReturnModel Create(IdentityRole appRole) {

        return new RoleReturnModel
       {
           Url = _UrlHelper.Link("GetRoleById", new { id = appRole.Id }),
           Id = appRole.Id,
           Name = appRole.Name
       };

    }
}

以下是您AccountController继承的BaseApiController.cs的摘录。

public class BaseApiController : ApiController
{

    private ModelFactory _modelFactory;
    private ApplicationUserManager _AppUserManager = null;
    private ApplicationRoleManager _AppRoleManager = null;

    protected ApplicationUserManager AppUserManager
    {
        get
        {
            return _AppUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
    }

    public BaseApiController()
    {
    }

    //...code removed for brevity

    protected ModelFactory TheModelFactory
    {
        get
        {
            if (_modelFactory == null)
            {
                _modelFactory = new ModelFactory(this.Request, this.AppUserManager);
            }
            return _modelFactory;
        }
    }
}

答案 1 :(得分:0)

您不必像在注册方法中那样返回201 Created响应。为简单起见,您只需返回一个200响应的OK()。

但是,如果您必须使用确切的Created响应来关注他的示例,则只需将以下代码粘贴到AccountController.cs文件的Helpers区域(或任何位置)

protected ModelFactory TheModelFactory
{
    get
    {
       if (_modelFactory == null)
       {
          _modelFactory = new ModelFactory(this.Request, UserManager);
       }
            return _modelFactory;
    }
}

此后你应该没事。