【问题标题】:Read Okta SAML response attribute from nuget package or opensource class从 nuget 包或开源类中读取 Okta SAML 响应属性
【发布时间】:2015-01-15 02:11:47
【问题描述】:

我正在阅读 Okta 对申请的回应如下。请让我知道是否有 nuget 包或开源类来读取属性。

        var xml = HttpContext.Request.Form["SAMLResponse"];
        byte[] byteData = Convert.FromBase64String(xml);
        string samlXmlString = Encoding.UTF8.GetString(byteData);
        var stream = new StringReader(samlXmlString);
        var xmlReader = XmlReader.Create(stream);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(samlXmlString);
        var emailElement = doc.GetElementsByTagName("saml2:NameID");
        var attributes = doc.GetElementsByTagName("saml2:Attribute"); 
        var attributesValues = doc.GetElementsByTagName("saml2:AttributeValue");
        var username = emailElement[0].InnerText;
        var role = attributesValues[0].InnerText;

【问题讨论】:

    标签: saml okta


    【解决方案1】:

    可以从以下代码中读取 SAML 响应属性。

    var responseDecoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(HttpUtility.HtmlDecode(HttpContext.Current.Request.Form["SAMLResponse"])));
    
            // Pick out the token
            using (StringReader sr = new StringReader(responseDecoded))
            {
                using (XmlReader reader = XmlReader.Create(sr))
                {
                    reader.ReadToFollowing("Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
    
                    // Deserialize the token so that data can be taken from it and plugged into the RSTR
                    SecurityTokenHandlerCollection coll = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
                    token = (Saml2SecurityToken)coll.ReadToken(reader.ReadSubtree());
                }
            }
    
            if (token != null)
            {
                UserName = token.Assertion.Subject.NameId.Value;
                Issuer = token.Assertion.Issuer.Value;
    
                var saml2Statement = token.Assertion.Statements.FirstOrDefault(x => x.GetType() == new Saml2AttributeStatement().GetType());
                if (saml2Statement != null)
                {
                    var attributes = ((Saml2AttributeStatement)saml2Statement).Attributes;
                    if (attributes != null)
                    {
                        if (attributes.FirstOrDefault(x => x.Name.ToString().ToLower().Equals("firstname")) != null)
                            FirstName = attributes.FirstOrDefault(x => x.Name.ToString().ToLower().Equals("firstname")).Values.FirstOrDefault();
    
                        if (attributes.FirstOrDefault(x => x.Name.ToString().ToLower().Equals("lastname")) != null)
                            LastName = attributes.FirstOrDefault(x => x.Name.ToString().ToLower().Equals("lastname")).Values.FirstOrDefault();
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      不要直接解析 SAML 响应。在 SAML 响应中检查 XML Signature关键。否则,未经授权的用户很容易使用 SAML 登录您的系统。

      我还没有找到一个好的 NuGet 或开源包来解析 .NET 中的 SAML。因此,我建议使用来自 ComponentSpace 的SAML v2.0 SSO component。配置 ComponentSpace 包后,解析 SAML 响应就这么简单:

      bool isInResponseTo = false;
      string partnerIdP = null;
      string userName = null;
      IDictionary<string, string> attributes = null;
      string targetUrl = null;
      
      try
      {
          SAMLServiceProvider.ReceiveSSO(Request, out isInResponseTo, out partnerIdP, out userName, out attributes, out targetUrl);
      }
      catch (ComponentSpace.SAML2.Exceptions.SAMLException exception)
      {
          throw exception;
      }
      

      在上面的示例代码中,来自 SAML 响应的属性将位于 attributes IDictionary 中。

      如果您将 SAML v2.0 SSO 组件安装到系统的默认位置,您可以在 C:\Program Files (x86)\ComponentSpace SAML v2.0 for .NET\Examples\SSO\HighLevelAPI\MVC\MvcExampleServiceProvider\Controllers 中找到更多示例

      【讨论】:

        猜你喜欢
        • 2018-06-30
        • 2017-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-15
        • 2015-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多