这是我想出的解决方案。它仍然不像我希望的那样清晰,它使用 cookie,所以欢迎添加任何内容。
首先,我必须安装 Microsoft.AspNet.Identity.Owin 包及其所有依赖项。
然后我按如下方式注册了我的身份验证:
private void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
然后在 Startup.cs 文件的 Configuration 方法中调用该方法。
为了使用身份验证,需要一个 IAuthenticationManager 实例。我将它注入我的控制器并使用 Ninject 来解决依赖关系
kernel.Bind<IAuthenticationManager>().ToMethod(_ => HttpContext.Current.GetOwinContext().Authentication).InRequestScope();
这是需要身份验证时将用户重定向到的 Account 控制器的 Login 方法(感谢 ConfigureAuth 方法中的 LoginPath):
[HttpPost]
public ActionResult Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = authenticationService.AuthenticateUser(model.Login);
IdentitySignIn(user.Id, user.Login);
return RedirectToAction("Index", "Home");
}
AuthenticationService 是我自己的类,它与数据库通信并执行登录以创建或返回用户。
IdentitySignIn 声明如下:
private void IdentitySignIn(int userId, string userLogin)
{
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.PrimarySid, userId.ToString()));
claims.Add(new Claim(ClaimTypes.Name, userLogin));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties()
{
ExpiresUtc = DateTime.UtcNow.AddDays(200),
IsPersistent = true
}, identity);
}
此方法创建一个带有适当信息的 cookie。不过,有一件事。当我查看cookie过期日期时,它不是当前日期加上200天,这有点尴尬。
SignOut 方法很简单:
public void IdentitySignout()
{
authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
}
因此,现在可以在 SignalR 集线器中使用 Identity.Name 属性访问 User.Identity。
要做的事情:通过 User.Identity.Id 之类的方式访问 Id 属性也很好。据我所知,它需要实现自定义主体。
我还在考虑使用 cookie 在客户端存储会话 ID 来实现我自己的某种会话,尽管它肯定会比使用 Identity 花费更多的时间。
补充:
为了获取用户 ID,可以使用 IdentityExtensions 的扩展方法:
(在集线器内)
Context.User.Identity.GetUserId()
为了使其工作,具有用户 id 值的声明应具有类型 ClaimTypes.NameIdentifier。
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.NameIdentifier, userId.ToString()));
claims.Add(new Claim(ClaimTypes.Name, userLogin));
更新 2:
这里有一些关于这个主题的附加链接,对我有很大帮助。我不包含指向 MS 指南的链接,因为它们很容易找到。
http://leastprivilege.com/2015/07/21/the-state-of-security-in-asp-net-5-and-mvc-6-claims-authentication/
http://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application