如果用这个链接丢失了以前的电子邮件,我需要重新发送给用户的激活链接。
所以,我在Login方法中检查了用户状态:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
bool isConfirmed = (model == null) ? false : WebSecurity.IsConfirmed(model.UserName);
string errorMsg = "Login or password is incorrect.";
if (isConfirmed)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
}
else
{
if (WebSecurity.UserExists(model.UserName))
{
errorMsg = "Your account is not activated. Click <a href=\"" + Url.Content("~/Account/ResendConfirmationLink") + "?id=" + model.UserName +"\" class=\"alert-link\">here</a> for resend email with activation link.";
}
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", errorMsg);
return View(model);
}
上面的代码生成http://localhost:64612/Account/ResendConfirmationLink?id=nickname
之类的链接,所以通过点击它我们应该调用以下方法(来自Account控制器):
[HttpGet]
public ActionResult ResendConfirmationLink(string id)
{
using (ChatContext chatContxt = new ChatContext())
{
var tsqlQuery = string.Format("SELECT [ConfirmationToken] FROM [webpages_Membership] WHERE [UserId] IN (SELECT [UserId] FROM [User] WHERE [UserName] LIKE '{0}')", id);
string confirmationToken = chatContxt.Database.SqlQuery<string>(tsqlQuery).First();
tsqlQuery = string.Format("SELECT [Email] FROM [User] WHERE [UserName] LIKE '{0}'", id);
string email = chatContxt.Database.SqlQuery<string>(tsqlQuery).First();
SendEmail(email, confirmationToken);
}
return View("Login");
}
但是永远不会调用方法ResendConfirmationLink()
!重新加载登录视图,并将网址转换为http://localhost:64612/Account/Login?ReturnUrl=%2fAccount%2fResendConfirmationLink%3fid%3dusername1&id=username1
。
我做错了什么?为什么方法不通过点击链接来调用?
答案 0 :(得分:5)
您需要在[AllowAnonymous]
操作中添加ResendConfirmationLink
注释。
它希望用户在调用您不想要的方法之前进行身份验证。
答案 1 :(得分:1)
你犯了两个错误,如下所示
首先,您必须将[AllowAnonymous]属性添加到ResendConfirmationLink方法。
您需要更改您的resedconfirmation网址
http://localhost:64612/Account/ResendConfirmationLink?username=nickname
到
http://localhost:64612/Account/ResendConfirmationLink?id=nickname