2015-11-14 80 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; 
    } 
} 

你應該在此之後罰款。

相關問題