Пример #1
0
        private static void InitializeSystemSecurityContext()
        {
            SettingsSecureFileStore securitySettings = new SettingsSecureFileStore();
            ServiceConfiguration    configs          = securitySettings.GetConfigurations();

            if (configs != null)
            {
                ServiceHelperFactory.get_Instance().set_BaseUri(configs.get_ServiceBaseUrl());
                ClientConfiguration settings = securitySettings.GetClientSettingsFromConfigDll();
                if (settings == null)
                {
                    Program.logger.Error("Client configurations for security service not found");
                }
                if (settings != null)
                {
                    settings.set_ActAsClientUrl(ServiceHelperFactory.get_Instance().get_BaseUri());
                    NetworkCredential       creds = CredentialCache.DefaultCredentials as NetworkCredential;
                    ClaimsPrincipalSelector claimsPrincipalSelector = new ClaimsPrincipalSelector(creds, settings, null);
                    claimsPrincipalSelector.GetClaimsPrincipal();
                    ClaimsPrincipal.ClaimsPrincipalSelector = new Func <ClaimsPrincipal>(claimsPrincipalSelector.GetClaimsPrincipal);
                    if (claimsPrincipalSelector.get_ClaimsPrincipal() != null)
                    {
                        claimsPrincipalSelector.get_ClaimsPrincipal().set_ActAsToken(ActiveClient.GetActAsToken(creds, settings, claimsPrincipalSelector.get_ClaimsPrincipal()));
                    }
                    ServiceHelperFactory.get_Instance().set_StsClientConfiguration(settings);
                    ServiceHelperFactory.get_Instance().set_StsUserCredentials(creds);
                }
            }
            else
            {
                Program.logger.Error("Client configurations not found");
            }
        }
Пример #2
0
        private static void InitializeSecurityContext(string tokenString)
        {
            tokenString = CryptographyHelper.DecryptFromLocalMachine(tokenString);
            SettingsSecureFileStore securitySettings = new SettingsSecureFileStore();
            ServiceConfiguration    configs          = securitySettings.GetConfigurations();

            if (configs != null)
            {
                SecurityToken token = Helper.DeSerializeSecurityToken(tokenString, configs.get_STSThumbprint(), configs.get_STSThumbprintName());
                ServiceHelperFactory.get_Instance().set_BaseUri(configs.get_ServiceBaseUrl());
                ClientConfiguration settings = securitySettings.GetClientSettingsFromConfigDll();
                if (settings == null)
                {
                    Program.logger.Error("Client configurations for security service not found");
                }
                if (settings != null)
                {
                    string clientIDFromToken         = string.Empty;
                    List <RequestClaim> customClaims = new List <RequestClaim>();
                    SamlSecurityToken   samlToken    = token as SamlSecurityToken;
                    if ((samlToken == null || samlToken.Assertion == null || samlToken.Assertion.Statements == null ? false : samlToken.Assertion.Statements.Count > 0))
                    {
                        SamlAttributeStatement statements = samlToken.Assertion.Statements[0] as SamlAttributeStatement;
                        if ((statements == null || statements.Attributes == null ? false : statements.Attributes.Count > 0))
                        {
                            SamlAttribute ClientId = statements.Attributes.FirstOrDefault <SamlAttribute>((SamlAttribute z) => StringUtility.EqualsIgnoreCase(z.Name, "clientid"));
                            if ((ClientId == null || ClientId.AttributeValues == null ? false : ClientId.AttributeValues.Count > 0))
                            {
                                clientIDFromToken = ClientId.AttributeValues[0];
                                customClaims.Add(new RequestClaim("http://schemas.imanami.com/ws/2014/06/identity/claims/clientId", true, clientIDFromToken));
                            }
                        }
                    }
                    settings.set_ActAsClientUrl(ServiceHelperFactory.get_Instance().get_BaseUri());
                    NetworkCredential       creds = CredentialCache.DefaultCredentials as NetworkCredential;
                    ClaimsPrincipalSelector claimsPrincipalSelector = (clientIDFromToken == string.Empty ? new ClaimsPrincipalSelector(creds, settings, null) : new ClaimsPrincipalSelector(creds, settings, customClaims));
                    claimsPrincipalSelector.GetClaimsPrincipal();
                    ClaimsPrincipal.ClaimsPrincipalSelector = new Func <ClaimsPrincipal>(claimsPrincipalSelector.GetClaimsPrincipal);
                    if (claimsPrincipalSelector.get_ClaimsPrincipal() != null)
                    {
                        claimsPrincipalSelector.get_ClaimsPrincipal().set_ActAsToken(token);
                    }
                    ServiceHelperFactory.get_Instance().set_StsClientConfiguration(settings);
                    ServiceHelperFactory.get_Instance().set_StsUserCredentials(creds);
                }
            }
            else
            {
                Program.logger.Error("Client configurations not found");
            }
        }