SP启动的SAML 2.0 POST通过C#Web API中符合标准的Idp元数据

时间:2018-03-20 10:21:06

标签: c# asp.net-web-api saml

我想整合SP发起的SAML 2.0 POST。此外,我需要生成服务提供商的元数据,以便将其共享给我的客户。请有人对此有所了解。示例代码会更有帮助。

1 个答案:

答案 0 :(得分:1)

经过研究,我发现用于C#Web API的Sustainsys.saml2是可用的最佳选项,几乎不需要编码,并且可以通过配置在配置文件中轻松集成。

因此,要实现该实现,您需要执行以下操作:

    可以通过cygwin中的openssl工具生成的
  1. X509证书。对于您的Web应用程序。

  2. 通过其进行身份验证的ADFS服务器

  3. 通过添加为应用程序生成的x509证书,为您的应用程序生成
  4. SAML元数据。 https://www.samltool.com/sp_metadata.php是可在此处轻松生成元数据的网站。

  5. 然后在Web配置文件中添加SAML的配置。有关更多信息,您可以访问https://github.com/Sustainsys/Saml2

  6. 然后将此代码添加到控制器中的index()

 if (User.Identity.IsAuthenticated)
            {
                var ssotype = WebConfigurationManager.AppSettings["SSOType"];
                if (!string.IsNullOrEmpty(ssotype) && ssotype == "onelogin")
{
                    var email = System.Security.Claims.ClaimsPrincipal.Current.Claims.FirstOrDefault(x => x.Type.Equals("User.email")).Value;
                    Session["loginUserId"] = email;
                }
                else
                {
                    var email = System.Security.Claims.ClaimsPrincipal.Current.Claims.FirstOrDefault(x => x.Type.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")).Value;
                    Session["loginUserId"] = email;
                }
                Response.Redirect("index.html",false);
            }
            else
            {
                String redirecturl = null;
                var idp = Sustainsys.Saml2.Configuration.SustainsysSaml2Section.Current.IdentityProviders.FirstOrDefault();
                var routevalues = new RouteValueDictionary();
                routevalues.Add("idp", idp.EntityId);
                redirecturl = "~/Saml2/SignIn?idp=" + HttpUtility.UrlEncode(idp.EntityId);
                Response.Redirect(redirecturl,false);
                
}