是否可以使用 OWIN 仅管理用户登录和注销?

Is it possible to use OWIN to manage only the user login and logout?

我想在 MVC5 中使用身份验证,就像在 MVC4 FormsAuthenticaiton.SignIn(...) 中使用的一样,但我不想使用 Forms Auth。在 OWIN 被设置为标准后,我在很多地方读到它如何更安全等等。

所以我所说的 "the same way" 的意思是能够只调用一个方法并说用户已登录或以相同的方式注销用户,例如 FormsAuthentication.SignInFormsAuthentication.SignOut.

我不想自定义 OWIN 的应用程序用户,因为我不会将我的 POCO 公开给我的 Web 项目,更不用说我的 DbContext(这也是高度自定义的)。我只想使用 OWIN,但只是为了管理用户会话状态(登录、会话、超时、注销)我不需要,也不想要 OWIN 的角色、用户等等。

我希望我已经说清楚了,如果没有,请告诉我有什么不清楚的地方,这样我就可以尝试改进它。

感谢任何帮助,这件事让我很烦! :(

好吧,经过大量代码检查并感谢 ReSharper 的反编译器,我得以弄清楚。 这是我的工作代码:

public static IAuthenticationManager GetAuthenticationManager(this Controller controller)
{
    return controller.HttpContext.GetOwinContext().Authentication;
}

public static void SignIn(this Controller controller, string username, string fullName, bool isPersistent = false)
{
    var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie, ClaimsIdentity.DefaultNameClaimType, null);

    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, username, ClaimValueTypes.String));
    identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, fullName, ClaimValueTypes.String));
    identity.AddClaim(new Claim("LastLogin", DateTime.Now.ToString(CultureInfo.CurrentCulture)));

    controller.GetAuthenticationManager().SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
}

public static void SignOut(this Controller controller)
{
    controller.GetAuthenticationManager().SignOut(DefaultAuthenticationTypes.ApplicationCookie);
}

我所做的是使用上面的这些方法创建一个扩展 class 并在我的控制器上使用它,我只是调用方法 SignIn 和 SignOut。 "secret"/答案是:

  • 检索 IAuthenticationManager
  • 使用我想要的登录信息创建一个 ClaimsIdentity
  • 使用 IAuthenticationManager 和 ClaimsIdentity 登录

就是这样。 =/