asp.net 身份验证通过 link / url asp.net mvc 5
asp.net identity authenticate by link / url asp.net mvc 5
我是 asp.net 身份框架的新手。有使用简单的会员提供者等的经验
我想通过使用特殊的 url 和一个令牌来验证用户身份。
喜欢:http://www.website.com/urlAuth/
一位用户收到了一封电子邮件,我想通过此 url
向用户 login/authenticate
令牌在一段时间内处于活动状态,并且有一个用户帐户和一些附加到它的操作。
我不知道如何在没有密码的情况下对用户进行身份验证。我调查了一个模拟场景(我在 web.config 中有一个特殊的身份验证信息模拟),但我认为这不是一个可靠的解决方案。
有人能帮我找到一个可靠的解决方案吗
谢谢!
您不需要密码来验证身份中的用户。只是您需要根据令牌找到相关用户。考虑这个简单的例子:
public ActionResult UrlAuth(string token)
{
var userManager=HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
// imaging you have a custom token manager which return userID form token
string userID=_tokenManager.GetUserIDByToken(token);
var user = userManager.FindById(userID);
if (user != null)
{
var ident = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, ident);
// authentication succeed do what you want
return Redirect("Wherever");
}
ModelState.AddModelError("", "Invalid username or password");
return View();
}
我是 asp.net 身份框架的新手。有使用简单的会员提供者等的经验
我想通过使用特殊的 url 和一个令牌来验证用户身份。 喜欢:http://www.website.com/urlAuth/
一位用户收到了一封电子邮件,我想通过此 url
向用户 login/authenticate令牌在一段时间内处于活动状态,并且有一个用户帐户和一些附加到它的操作。
我不知道如何在没有密码的情况下对用户进行身份验证。我调查了一个模拟场景(我在 web.config 中有一个特殊的身份验证信息模拟),但我认为这不是一个可靠的解决方案。
有人能帮我找到一个可靠的解决方案吗
谢谢!
您不需要密码来验证身份中的用户。只是您需要根据令牌找到相关用户。考虑这个简单的例子:
public ActionResult UrlAuth(string token)
{
var userManager=HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
// imaging you have a custom token manager which return userID form token
string userID=_tokenManager.GetUserIDByToken(token);
var user = userManager.FindById(userID);
if (user != null)
{
var ident = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, ident);
// authentication succeed do what you want
return Redirect("Wherever");
}
ModelState.AddModelError("", "Invalid username or password");
return View();
}