将 mvc 5 adfs 转换为 .net 核心 adfs

converting mvc 5 adfs to .net core adfs

我有一个现有的 mvc 5 应用程序,它成功地使用了本地活动目录联合服务

相关的网络配置设置

 <appSettings>
    <add key="ida:Issuer" value="https://www.fedsvc3copa.beta.pa.gov/adfs/ls/"/>
  </appSettings>

 <authority name="http://www.fedsvc3copa.beta.pa.gov/adfs/services/trust">
          <keys>
            <add thumbprint="xxxxxxxxxxxxxxx"/>
          </keys>
          <validIssuers>
            <add name="http://www.fedsvc3copa.beta.pa.gov/adfs/services/trust"/>
          </validIssuers>
        </authority>

           <federationConfiguration>
      <cookieHandler requireSsl="true"/>

      <wsFederation passiveRedirectEnabled="true" issuer="https://www.fedsvc3copa.beta.pa.gov/adfs/ls/" realm="https://localhost:44363/" requireHttps="true"/>
    </federationConfiguration>

尝试为 .net 核心 mvc 应用程序做同样的事情。但我有点困惑该放什么 startup.cs

我跟着https://docs.microsoft.com/en-us/aspnet/core/security/authentication/ws-federation?view=aspnetcore-2.1

所以我有

 .AddWsFederation(options =>
      {
        // MetadataAddress represents the Active Directory instance used to authenticate users.
        options.MetadataAddress = "https://www.fedsvc3copa.beta.pa.gov/federationmetadata/2007-06/FederationMetadata.xml";

        // Wtrealm is the app's identifier in the Active Directory instance.
        // For ADFS, use the relying party's identifier, its WS-Federation Passive protocol URL:
        options.Wtrealm = "https://localhost:44363/";

        // For AAD, use the App ID URI from the app registration's Properties blade:
        options.Wtrealm = "???????";
      });

我不确定要在 AAD 领域中放入什么,因为我没有使用 azure。我也不需要指纹和发行人吗? http://www.fedsvc3copa.beta.pa.gov/adfs/services/trust

回答你的第一个问题:

如果您不使用 Azure,则无需担心 AAD。事实上,您要确保 .Wtrealm 没有配置两次。所以只删除第二个。

回答关于指纹和发行人的第二个问题:

我认为您不需要这些值,但它们可能很好,因为指纹和发行者值用于验证令牌。

我已尝试在属于 startup.cs 文件的以下代码中复制您所有的原始配置设置。可以从 MetadataAddress url 的 xml 文件中检索 your x.509 cert string 值。它将位于 <X509Certificate> 标签之间。

var rawCertData = Convert.FromBase64String("your x.509 cert string");
X509Certificate2 cert = new X509Certificate2(rawCertData);
SecurityKey signingKey = new X509SecurityKey(cert);
    services.AddAuthentication()
        .AddWsFederation(options => {
            options.MetadataAddress = "https://www.fedsvc3copa.beta.pa.gov/federationmetadata/2007-06/FederationMetadata.xml";
            options.Wtrealm = "https://localhost:44363/";
            options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters {
                ValidateIssuer = true,
                ValidIssuer = "http://www.fedsvc3copa.beta.pa.gov/adfs/services/trust",
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = signingKey
            };
            options.RequireHttpsMetadata = true;
        }).AddCookie(cookieoption => {
            cookieoption.Cookie.SecurePolicy = CookieSecurePolicy.Always;
        });

注意:通过此配置,我可以进入您的 adfs 登录页面。但是,我无法登录,因为我没有权限;所以我不知道您登录后 POST 上会发生什么。如果您有任何问题,请随时告诉我。