【问题标题】:How to configure and get custom attribute in okta with saml2.0如何使用 saml2.0 在 okta 中配置和获取自定义属性
【发布时间】:2016-11-07 05:01:13
【问题描述】:

我在我的应用程序中使用 okta 作为 idp,我想配置自定义属性,例如:ID,如何在 okta 中完成?以及如何在 okta 中设置这些值?

【问题讨论】:

    标签: spring-boot saml-2.0 okta


    【解决方案1】:

    以下是向 Okta 的 SAML 断言添加自定义属性的过程:

    1. 从您的 Okta 组织的仪表板转到管理 -> 目录 -> 配置文件编辑器
    2. 在“Okta”配置文件中,选择“配置文件”按钮

    1. 确定您要添加的用户属性的“变量名称”(而不是“显示名称”)值。例如,让我们尝试添加“title”属性。

    1. 导航到“应用程序”选项卡并选择您要添加此自定义属性的 SAML 应用程序。
    2. 选择“常规”标签
    3. 在“SAML 设置”设置中,按“编辑”按钮。这应该会启动应用配置向导,就像它是一个新的 SAML 应用一样
    4. 按下一步按钮并向下滚动到“属性语句(可选)”部分
    5. 按“添加另一个”按钮

    1. 在第一个文本框中,输入您希望在应用中使用的 SAML 属性的名称,该名称将在您的 SAML 断言中可用(我选择了“jobTitle”)。在第二个文本框中,输入 Okta 配置文件中的变量名称,前缀为“user”。 (例如“user.title”)

    1. 按“下一步”,然后按“完成”

    当您测试您的应用时,您应该获得以下 SAML AttributeStatement 节点:

    <saml2:AttributeStatement xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">
            <saml2:Attribute Name="firstName"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >Isaac</saml2:AttributeValue>
            </saml2:Attribute>
            <saml2:Attribute Name="lastName"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >Brock</saml2:AttributeValue>
            </saml2:Attribute>
            <saml2:Attribute Name="Email"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >isaac.brock@mailinator.com</saml2:AttributeValue>
            </saml2:Attribute>
            <saml2:Attribute Name="userName"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >isaac@company.com</saml2:AttributeValue>
            </saml2:Attribute>
            <saml2:Attribute Name="phone"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >+1 415 456 7893</saml2:AttributeValue>
            </saml2:Attribute>
            <saml2:Attribute Name="jobTitle"
                             NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"
                             >
                <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema"
                                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                      xsi:type="xs:string"
                                      >Vice President</saml2:AttributeValue>
            </saml2:Attribute>
        </saml2:AttributeStatement>
    

    (注意最后一个“jobTitle”属性)

    我希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      您可以使用 SDK 方法来获取然后修改 IAppUser 的自定义属性。您可以使用以下 SDK 方法获取用户。这些是异步方法。您可以使用以下 2 种方法来获取:-

              public async Task<IAppUser> GetOktaApplicationUser(string oktaProfileId)
              {
                 var x = await 
              Client.Applications.**GetApplicationUserAsync**(ConfigurationManager.AppSettings["okta:ClientId"], 
                                oktaProfileId);
                 return x;
              }
         var userApp = client.GetOktaApplicationUser(oktaProfileId).Result;
         var userWithCustomAttributes = userApp.**GetData**();   //Getdata() to get Custom Attributes of User
      

      你可以使用jsonConvert来序列化它,这样你就可以反序列化它来获取你自己的Model(class)

      中的数据
         string json = JsonConvert.SerializeObject(userWithCustomAttributes, Formatting.Indented);
            userWithCustomModel = JsonConvert.DeserializeObject<"CustomModel">(json);
      

      上面的方法是获取用户..然后你可以使用SetProperty()IAppUser的方法修改并发送修改后的用户,如下:-

       public async Task<bool> UpdateApplicationUser(CustomModel user)
              {
                  **IAppUser** appuser = new Okta.Sdk.AppUser();
                  appuser.Profile = new Resource();
                  appuser.Id = user.id;
                  appuser.Profile.**SetProperty**("email", user.profile.email);
                  appuser.Profile.**SetProperty**("<"Your custom attribute name">", user.profile.Roles);                                                                                                              
                  try
                  {
                      var x = await Client.Applications.**UpdateApplicationUserAsync**(appuser, ConfigurationManager.AppSettings["okta:ClientId"], appuser.Id);
                      return true;
                  }
      

      UpdateApplicationUserAsync 此方法将修改您使用setproperty() 设置的自定义属性。在setproperty() 第一个参数中,我使用了字符串常量,您可以根据特定自定义属性的映射变量名称使用自己的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多