2015-11-14 63 views
2

我想使用Identity 2使用Visual Studio中的当前模板构建一个ASP.net Web API。使用Identity 2确认Web API 2中的电子邮件的最佳方式是什么?

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

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

我发现 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)为了让它起作用,我该怎么做?

回答

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 
     }; 

    } 
} 

这里是BaseApiController.cs从您AccountController继承的一个片段。

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; 
     } 
    } 
} 
0

您不必像他那样在注册方法中返回201创建的响应。为了简单起见,你可以返回一个OK(),它是一个200响应。

但是,如果必须使用精确创建的响应以他为榜样,你可以简单地粘贴AccountController.cs的下面这段代码在助手区域(或只是任何地方)文件

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

你应该在此之后罚款。

相关问题