如何通过单击 link 从控制器调用方法
How to call method from controller by clicking on link
如果用户丢失了之前包含此 link 的电子邮件,我需要向他重新发送激活 link。
所以,我在登录方法中检查了用户状态:
[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);
}
上面的代码生成 link 就像 http://localhost:64612/Account/ResendConfirmationLink?id=nickname
所以通过点击它我们应该调用下面的方法(从账户控制器):
[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()
!重新加载登录视图,url 转换为 http://localhost:64612/Account/Login?ReturnUrl=%2fAccount%2fResendConfirmationLink%3fid%3dusername1&id=username1
。
我做错了什么?为什么方法不通过单击 link 调用?
您需要将 [AllowAnonymous]
注释添加到您的 ResendConfirmationLink
操作中。
它希望在调用该方法之前对用户进行身份验证,而这是您不希望的。
你犯了以下两个错误
首先,您必须将 [AllowAnonymous] 属性添加到 ResendConfirmationLink 方法。
您需要更改您的重新确认url
http://localhost:64612/Account/ResendConfirmationLink?username=nickname
至
http://localhost:64612/Account/ResendConfirmationLink?id=nickname
如果用户丢失了之前包含此 link 的电子邮件,我需要向他重新发送激活 link。
所以,我在登录方法中检查了用户状态:
[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);
}
上面的代码生成 link 就像 http://localhost:64612/Account/ResendConfirmationLink?id=nickname
所以通过点击它我们应该调用下面的方法(从账户控制器):
[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()
!重新加载登录视图,url 转换为 http://localhost:64612/Account/Login?ReturnUrl=%2fAccount%2fResendConfirmationLink%3fid%3dusername1&id=username1
。
我做错了什么?为什么方法不通过单击 link 调用?
您需要将 [AllowAnonymous]
注释添加到您的 ResendConfirmationLink
操作中。
它希望在调用该方法之前对用户进行身份验证,而这是您不希望的。
你犯了以下两个错误
首先,您必须将 [AllowAnonymous] 属性添加到 ResendConfirmationLink 方法。
您需要更改您的重新确认url
http://localhost:64612/Account/ResendConfirmationLink?username=nickname
至
http://localhost:64612/Account/ResendConfirmationLink?id=nickname