C# 具有Windows身份验证的SessionAuthenticationModule

C# 具有Windows身份验证的SessionAuthenticationModule,c#,asp.net,asp.net-mvc,wif,claims-based-identity,C#,Asp.net,Asp.net Mvc,Wif,Claims Based Identity,我正在使用Windows身份验证,因为我的ASP.NET MVC 5应用程序正在内部网中运行(Active Directory是主要的身份验证颁发者) 由于广告和浏览器在每次请求时都会不断地进行401握手,所以网站的速度相当慢 我现在正在研究一种缓存从AD收到的声明的方法,以便获得一些性能增益 这在WIF和SessionAuthenticationModule中可行吗?一旦生成SessionSecurityToken,它会停止对AD的后续请求吗?在ASP.NET管道中创建这样一个令牌的最佳位置是

我正在使用Windows身份验证,因为我的ASP.NET MVC 5应用程序正在内部网中运行(Active Directory是主要的身份验证颁发者)

由于广告和浏览器在每次请求时都会不断地进行401握手,所以网站的速度相当慢

我现在正在研究一种缓存从AD收到的声明的方法,以便获得一些性能增益

这在WIF和SessionAuthenticationModule中可行吗?一旦生成SessionSecurityToken,它会停止对AD的后续请求吗?在ASP.NET管道中创建这样一个令牌的最佳位置是什么

  • 是的,这对山姆来说是可行的
  • 是的,你不必在初次转型后每次都点击广告

  • 你应该做的是

  • 在您的站点上实施ClaimsAuthenticationManager。这将拦截您的第一个请求,并接收您的初始请求。最初的ClaimsPrincipal将只包含您的用户名和您的广告组的SID

  • 在ClaimsAuthenticationManager的实现中,您将从AD中读取特定于应用程序的声明,并创建一个新的转换ClaimsPrincipal。然后将其序列化为身份验证Cookie

  • 后续请求将被SAM拦截,您不需要从AD进行索赔转换,因为您第一次已经这样做了

  • 这就是你如何做到的:

  • 创建一个ClaimsAuthenticationManager,如下所示:

    public class AdClaimsAppender : ClaimsAuthenticationManager
      {
        public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
    {
          if (!incomingPrincipal.Identity.IsAuthenticated)
          {
              return base.Authenticate(resourceName, incomingPrincipal);
          }
          var adClaims = GetExtraAdClaims(incomingPrincipal);
          var newClaimsIdentity = new ClaimsIdentity(adClaims, "Windows");
          var newClaimsPrincipal = new ClaimsPrincipal(newClaimsIdentity);
         return base.Authenticate(resourceName, newClaimsPrincipal);
    }
    
    } }

  • 将SAM模块添加到web配置中

        <system.webServer>
        <modules>
        <add name="SessionAuthenticationModule"
                type="System.IdentityModel.Services.SessionAuthenticationModule,  System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089" preCondition="managedHandler"/>
         </modules>
        </system.webServer>
    

  • 将AdClaimsAppender挂接在何处?e.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager=new AdClaimsAppender();当我第一次读到你的答案时,我有了希望。但是,唉,这对我也不起作用。
    using System.IdentityModel.Services;
     using System.IdentityModel.Services.Configuration;
    
    
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          RouteConfig.RegisterRoutes(RouteTable.Routes);
            FederatedAuthentication.FederationConfigurationCreated += FederatedAuthentication_FederationConfigurationCreated;
      }
    
    private static void FederatedAuthentication_FederationConfigurationCreated(object sender, FederationConfigurationCreatedEventArgs e)
    {
        //from appsettings...
        const string domain = "";
        const bool requireSsl = false;
        const string authCookieName = "YourSiteAuth"; //default is fedauth, i normally create my own name as it is easier to identify when you have a lot of cookies.
    
        e.FederationConfiguration.CookieHandler = new ChunkedCookieHandler
            {
                Domain = domain,
                Name = authCookieName,
                RequireSsl = requireSsl,
                PersistentSessionLifetime = new TimeSpan(0, 0, 30, 0)
            };
    }