asp.net-mvc-4 - 为什么将 FormsAuthenticationTicket 与 Windows 身份验证结合使用?

标签 asp.net-mvc-4 cookies forms-authentication windows-authentication

我一直在网上阅读大量有关身份验证和授权的内容,并最终确定了一些似乎有效的代码......但我并不完全理解它所做的一切(就 FormsAuthenticationTicket 而言)。

我正在使用的 MVC 应用程序将处理一些敏感数据,我想仔细检查我所做的与身份验证和授权相关的所有操作。

我正在使用 Windows 身份验证;

<authentication mode="Windows" />
<authorization>    
  <deny users="?"/>
</authorization>

我在 SQL Server 中有一组表,其中包含其他用户和权限信息。

  • 用户
  • 角色
  • 用户角色
  • 定义应用程序中的对象和关联的其他表 权限。

在我的 Global.asax 中,我有以下受启发的方法

http://www.codeproject.com/Articles/5182/Insight-into-Security-Model-using-Principal-and-Id

protected void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
{
  if (e.Identity == null || !e.Identity.IsAuthenticated)
  {
    //Redirect to error or access denied page
  }

  string userData = e.Identity.AuthenticationType;          

  var cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;

  if (cachedUser == null)
  {
    var user = repo.GetUserByFullUserName(e.Identity.Name);
    HttpContext.Current.Cache.Insert(e.Identity.Name, user, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration);
    cachedUser = HttpContext.Current.Cache[e.Identity.Name] as User;
  }

  var userIdentity = e.Identity.Name;

  var formsAuthTicket = new FormsAuthenticationTicket(1, e.Identity.Name, DateTime.Now, DateTime.Now.AddMinutes(2), false, userData);

  var encryptedTicket = FormsAuthentication.Encrypt(formsAuthTicket);
  var httpcook = new HttpCookie("authCookie", encryptedTicket);
  Response.Cookies.Add(httpcook);
}



protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
  if (Request.IsAuthenticated)
  {
    var httpcook = Context.Request.Cookies["authCookie"];
    var formsAuthTicket = FormsAuthentication.Decrypt(httpcook.Value);

    var cachedUser = GetCachedUser(formsAuthTicket.Name);
    if (cachedUser == null)
      {
      cachedUser = CreateCachedUser(formsAuthTicket.Name);
      } 

    var genIdentity = new GenericCustomIdentity(cachedUser, Request.IsAuthenticated, formsAuthTicket.UserData);

    var genPrincipal = new GenericCustomPrincipal(genIdentity, cachedUser);

    HttpContext.Current.User = genPrincipal;
  }
}

这是我的问题:

  1. 为什么 WindowsAuthentication_OnAuthenticate 方法中有 FormsAuthenticationTicket?我不能在 WinAuth 方法中构建我的 Identity 和 Primary 对象吗?

  2. 将用户数据存储在 HttpContext.Current.Cache 中是否存在安全风险?由于这些方法被多次调用,我不想每次请求都访问数据库。有更好/更安全的替代方案吗?

  3. 我不熟悉使用 FormsAuthenticationTickets、Identity 和 Principal,因此我们将不胜感激任何意见或建议。抱歉,这不是一个问题。

最佳答案

Why have a FormsAuthenticationTicket in the WindowsAuthentication_OnAuthenticate method? Can't I just build my Identity and Principal objects in the WinAuth method?

身份和主体对象仅在客户端当前调用/请求期间存在。它们是 HttpContext 的一部分由于缺乏更好的术语,并且在请求结束时处置。一旦经过身份验证的人再次连接,就会创建一个新请求,并且默认情况下不会对他们进行身份验证。

FormsAuthenticationTicket (默认情况下)在客户端使用 cookie 来存储每个后续请求的身份验证信息。这允许 Application_AuthenticateRequest 方法使用 cookie 重新授权请求。

Is storing User data in HttpContext.Current.Cache a security risk? Since these methods are called numerous times I don't want to hit the db every single request. Is there a better/more secure alternative?

HttpContext.Cache存储在内存中。如果服务器重置或应用程序池因任何原因重新启动,缓存就会消失。

是的,这是一个安全风险,您存储用户信息的是服务器内存。然而,风险非常小,除非有人拥有您的代码来查看您如何使用缓存,并且可以上传代码来读取缓存。

I am unfamiliar with using FormsAuthenticationTickets, Identity, and Principal so any comments or suggestions would be greatly appreciated. Sorry, this wasn't a question.

链接到 HttpContext.User (IPrincipal) 怎么样?其属性 Identity 类型为 IIdentity 。这不是最具描述性的答案,但这两个界面都非常基本。

关于asp.net-mvc-4 - 为什么将 FormsAuthenticationTicket 与 Windows 身份验证结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17098426/

相关文章:

apache - JSESSIONID cookie 域

javascript - Cookie 在 Safari 中不起作用

c# - ServiceStack API 和 ASP MVC Authentication 两种方式

asp.net-mvc-4 - ClaimsAuthorizationManager.CheckAccess : Handling false without going to login page

javascript - ASP.Net MVC 模型绑定(bind)到 javascript

javascript - 禁用第三方 cookie 是否也会禁用第三方 javascript 创建的 cookie?

asp.net - 当用户具有多个角色时,位置授权如何工作?

c# - 在 Asp.Net MVC 3 中,什么构成 AuthorizeAttribute 的经过身份验证的用户?

c# - 如何将 List<model> 从 MVC4 中的 View 传递到 Controller ?

c# - 未指定 asp.net mvc 4 默认路由?