1

我是 asp.net 身份框架的新手。有简单的会员提供者等方面的经验。

我想通过使用特殊 url 和令牌来获得用户身份验证。喜欢: http ://www.website.com/urlAuth/

用户收到一封电子邮件,我希望用户使用此 url 登录/验证

令牌在一段时间内处于活动状态,并且有一个用户帐户和一些附加到它的操作。

我无法弄清楚如何在没有密码的情况下对用户进行身份验证。我调查了一个模拟场景(我对 web.config 中的身份验证信息有一个特殊的模拟),但我认为这不是一个可靠的解决方案。

可以帮助我指导我找到一个可靠的解决方案吗

谢谢!

4

1 回答 1

2

您不需要密码来验证 Identity 中的用户。只需要根据令牌找到相关用户。考虑这个简单的例子:

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();
}
于 2015-09-15T14:10:39.087 回答