使用 Identity 2 在 Web API 2 中确认电子邮件的最佳方法是什么?
What is the best way to Confirm Email in Web API 2 using Identity 2?
我想使用身份 2 使用 Visual Studio 中的当前模板构建一个 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)
来让它工作?
您不必像他那样在您的注册方法中 return 201 Created 响应。为简单起见,您可以 return 一个 OK() ,它是一个 200 响应。
但是,如果您必须按照他的示例使用确切的 Created 响应,您只需将此代码粘贴到下面 AccountController.cs 文件
的 Helpers 区域(或任何位置)
protected ModelFactory TheModelFactory
{
get
{
if (_modelFactory == null)
{
_modelFactory = new ModelFactory(this.Request, UserManager);
}
return _modelFactory;
}
}
之后你应该会没事的。
文中有个link带你到GitHub
上的源码
这里是 link 到 ModelFactory.cs,它用于创建模型并将您想要的信息复制到 return 到它。
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;
}
}
}
我想使用身份 2 使用 Visual Studio 中的当前模板构建一个 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)
来让它工作?
您不必像他那样在您的注册方法中 return 201 Created 响应。为简单起见,您可以 return 一个 OK() ,它是一个 200 响应。
但是,如果您必须按照他的示例使用确切的 Created 响应,您只需将此代码粘贴到下面 AccountController.cs 文件
的 Helpers 区域(或任何位置)protected ModelFactory TheModelFactory
{
get
{
if (_modelFactory == null)
{
_modelFactory = new ModelFactory(this.Request, UserManager);
}
return _modelFactory;
}
}
之后你应该会没事的。
文中有个link带你到GitHub
上的源码这里是 link 到 ModelFactory.cs,它用于创建模型并将您想要的信息复制到 return 到它。
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;
}
}
}