Asp.net Core联合ADFS和Web应用程序代理
创始人
2024-09-15 21:30:44
0

要将ASP.NET Core与ADFS和Web应用程序代理集成在一起,可以按照以下步骤操作:

  1. 配置ADFS作为身份提供程序:

    • 在ASP.NET Core应用程序的Startup.cs文件中,添加以下代码:
      services.AddAuthentication().AddWsFederation(options =>
      {
          options.MetadataAddress = "https://your-adfs-server/FederationMetadata/2007-06/FederationMetadata.xml";
          options.Wtrealm = "https://your-app-url";
      });
      
  2. 配置Web应用程序代理:

    • 在ASP.NET Core应用程序的Startup.cs文件中,添加以下代码:
      app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent());
      app.UseHttpsRedirection();
      
      app.Use((context, next) =>
      {
          context.Request.Scheme = "https";
          return next();
      });
      
      app.Use((context, next) =>
      {
          context.Request.Headers["X-Forwarded-Host"] = context.Request.Host.Host;
          context.Request.Headers["X-Forwarded-Proto"] = "https";
          return next();
      });
      
      app.UseForwardedHeaders(new ForwardedHeadersOptions
      {
          ForwardedHeaders = ForwardedHeaders.XForwardedHost | ForwardedHeaders.XForwardedProto
      });
      
  3. 配置ASP.NET Core应用程序以使用ADFS进行身份验证:

    • 在ASP.NET Core应用程序的Startup.cs文件的ConfigureServices方法中,添加以下代码:
      services.Configure(options =>
      {
          options.KnownProxies.Add(IPAddress.Parse("127.0.10.1"));
      });
      
      services.Configure(options =>
      {
          options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
      });
      
      services.AddAuthentication(options =>
      {
          options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
          options.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
      })
      .AddWsFederation(options =>
      {
          options.MetadataAddress = "https://your-adfs-server/FederationMetadata/2007-06/FederationMetadata.xml";
          options.Wtrealm = "https://your-app-url";
      })
      .AddCookie();
      
      services.AddMvc();
      
  4. 创建一个控制器和视图以处理登录和注销:

    • 在ASP.NET Core应用程序中,创建一个名为AccountController的控制器,并添加以下代码:

      [AllowAnonymous]
      public class AccountController : Controller
      {
          public async Task Login(string returnUrl = "/")
          {
              await HttpContext.ChallengeAsync(WsFederationDefaults.AuthenticationScheme,
                  new AuthenticationProperties { RedirectUri = returnUrl });
      
              return new EmptyResult();
          }
      
          [AllowAnonymous]
          public async Task Logout()
          {
              await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
              await HttpContext.SignOutAsync(WsFederationDefaults.AuthenticationScheme);
      
              return RedirectToAction("Index", "Home");
          }
      }
      
    • 创建一个名为Login.cshtml的视图,并添加以下代码:

      Login

      Login with ADFS
  5. 配置路由:

    • 在ASP.NET Core应用程序的Startup.cs文件的Configure方法中,添加以下代码:
      app.UseRouting();
      
      app.UseAuthentication();
      app.UseAuthorization();
      
      app.UseEndpoints(endpoints =>
      {
          endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=Home}/{action=Index}/{id?}");
      });
      

这样,您的ASP.NET Core应用程序就可以与ADFS和Web应用程序代理集成在一起了。在浏览器中访问应用程序时,将会重定向到ADFS进行身份验证,并将身份验证成功后的用户信息返回给应用程序。

相关内容

热门资讯

线上(wepoke真的)原来是... 线上(wepoke真的)原来是真的有挂!其实真的有挂(2022已更新)(哔哩哔哩);亲,其实确实真的...
两教程(Wepoke程序)软件... 两教程(Wepoke程序)软件透明挂辅助工具(软件透明挂)透视辅助(2024已更新)(哔哩哔哩);致...
软件(wepoke透明)原来是... 软件(wepoke透明)原来是真的有挂!其实真的有挂(2020已更新)(哔哩哔哩)是一款可以让一直输...
一模拟器(德扑工具)外挂辅助工... 一模拟器(德扑工具)外挂辅助工具(透视)透视辅助(2025已更新)(哔哩哔哩);亲真的是有正版授权,...
系统(aapoker讲解)竟然... 系统(aapoker讲解)竟然真的有挂!其实真的有挂(2021已更新)(哔哩哔哩);aapoker讲...
6系统(aapoker下载)外... 6系统(aapoker下载)外挂辅助工具(辅助挂)透视辅助(2023已更新)(哔哩哔哩)aapoke...
智能(德扑之星刷数据)果真真的... 智能(德扑之星刷数据)果真真的有挂!原来真的有挂(2025已更新)(哔哩哔哩);《WPK辅助透视》‌...
1机器人(德州nzt软件)软件... 1机器人(德州nzt软件)软件透明挂辅助软件(透视)透视辅助(2022已更新)(哔哩哔哩);人气非常...
ai代打(德扑之星决策)确实是... ai代打(德扑之星决策)确实是真的有挂!原来真的有挂(2020已更新)(哔哩哔哩);科技详细教程小薇...
第8透明(wepoke数据)外... 第8透明(wepoke数据)外挂透明挂辅助神器(辅助挂)透视辅助(2023已更新)(哔哩哔哩);原来...