Asp.net 在Web API 2中使用Identity 2确认电子邮件的最佳方式是什么?
我想使用Visual Studio中的当前模板,使用Identity 2构建一个ASP.net Web API 我想确认创建帐户时使用的电子邮件地址是有效的电子邮件地址 我真的找不到多少关于如何使用当前模板的指南 我发现 这是从零开始构建的,但是事情非常混乱。我正在查看AccountController注册操作的代码Asp.net 在Web API 2中使用Identity 2确认电子邮件的最佳方式是什么?,asp.net,asp.net-web-api,asp.net-identity-2,Asp.net,Asp.net Web Api,Asp.net Identity 2,我想使用Visual Studio中的当前模板,使用Identity 2构建一个ASP.net Web API 我想确认创建帐户时使用的电子邮件地址是有效的电子邮件地址 我真的找不到多少关于如何使用当前模板的指南 我发现 这是从零开始构建的,但是事情非常混乱。我正在查看AccountController注册操作的代码 string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id); var
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));
string code=wait this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl=newuri(Url.Link(“confirmeailroute”,new{userId=user.Id,code=code}));
等待.AppUserManager.sendmailasync(user.Id,“确认您的帐户”,“请通过单击确认您的帐户”);
urilocationheader=newuri(Url.Link(“GetUserById”,new{id=user.id}));
返回已创建(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, );
//有关如何启用帐户确认和密码重置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=320771
//使用此链接发送电子邮件
字符串代码=wait UserManager.generateemailconfimationtokenasync(user.Id);
代码=HttpUtility.UrlEncode(代码);
var callbackUrl=newuri(Url.Link(“confirmeailroute”,new{userId=user.Id,code=code}));
等待UserManager.SendEmailAsync(user.Id,“确认您的帐户”,“请通过单击确认您的帐户”);
urilocationheader=newuri(Url.Link(“GetUserById”,new{id=user.id}));
已创建退货(locationHeader,);
他的模型库,我哪儿都找不到。我能够找到他为自己的项目创建的BaseApiController:ApiController
中提到的模型工厂,但在我的一生中,我始终无法找出它在当前带有个人用户帐户模板的Web Api中的位置
我应该把什么作为
T Content
而不是modelFactory.Create(user)
来实现这个功能呢?你不必像他那样在注册方法中返回201 Created response。为简单起见,您只需返回一个OK(),这是一个200响应
但是,如果您必须使用准确创建的响应来遵循他的示例,您可以简单地将此代码粘贴到AccountController.cs文件的Helpers区域(或任何位置)中
protected ModelFactory TheModelFactory
{
get
{
if (_modelFactory == null)
{
_modelFactory = new ModelFactory(this.Request, UserManager);
}
return _modelFactory;
}
}
在这之后你应该会没事的。文章中有一个链接,可以将你带到GitHub上的源代码 以下是指向的链接,用于创建模型和复制要返回的信息
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
继承自的一个片段
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;
}
}
}
公共类BaseApiController:ApicController
{
私人模型工厂(ModelFactory);;
私有ApplicationUserManager\u AppUserManager=null;
私有应用程序角色管理器_ApprovalManager=null;
受保护的ApplicationUserManager AppUserManager
{
得到
{
返回_AppUserManager??Request.GetOwinContext().GetUserManager();
}
}
公共BaseApiController()
{
}
//…为简洁起见,删除了代码
受保护的模型工厂模型工厂
{
得到
{
如果(_modelFactory==null)
{
_modelFactory=新的modelFactory(this.Request,this.AppUserManager);
}
返回工厂;
}
}
}
我同意你的看法,因为你永远不应该告诉任何点击api的人,如果他们使用了正确的密码,例如,应该只是标准的Ok响应。