1

有没有办法为应用程序池身份用户(配置用户)获取 SAML 令牌?

当我们配置应用程序池时,实体将配置条目(用户名和密码)存储在 applicationHost.config 中的 %systemroot%\System32\Inetsrv\config 路径下。

当应用程序启动时,它会选择用户名和加密密码进行身份验证。身份验证成功后,后续调用将遵循基于令牌的身份验证还是始终遵循基本身份验证?

如果它基于令牌,那么在第一次响应之后,我如何获取应用程序池身份用户的 SAML 令牌?

如果有任何链接,请告诉我。

提前致谢。

4

3 回答 3

0

Ans 1:通过使用 Adal 流获取登录用户的 Jwt 令牌,

if (!AdfsConfiguration.IsInitialized) throw new SecurityException(Constants.AdfsConfigurationInitilizationExceptionMessage);
if (string.IsNullOrEmpty(AdfsConfiguration.AdfsAuthorityUrl)) throw new SecurityException(Constants.AdfsConfigurationAdfsAuthorityUrlInitilizationExceptionMessage);

try
{
    var authenticationContext = new AuthenticationContext(string.Format(AdfsConfiguration.AdfsAuthorityUrl, AdfsConfiguration.AdfsInstance, AdfsConfiguration.Resource), false);

    var asyncRequest = authenticationContext.AcquireTokenAsync(AdfsConfiguration.Resource, AdfsConfiguration.ClientId, new Uri(AdfsConfiguration.RedirectUri), new PlatformParameters(PromptBehavior.Auto));
    var accessToken = asyncRequest.Result.AccessToken;
    return accessToken;
}
catch (Exception exp)
{
    var additionalInfo = $" additionalInfo : [authenticationContext : {string.Format(AdfsConfiguration.AdfsAuthorityUrl, AdfsConfiguration.AdfsInstance, AdfsConfiguration.Resource)}]";
    throw new SecurityException($"AdfsAuthorization.GetAdfsOAuthJwtAccessTokenForWinAppUserUsingAdal is failed, {additionalInfo}", exp);
}
于 2017-03-13T11:51:42.840 回答
0

Ans 2:通过 Auth 代码流获取登录用户或应用程序池身份用户的 Jwt 令牌。

第 1 步:从 Adfs 服务器获取 Auth 代码

        var authUrl = string.Format(AdfsConfiguration.AdfsAuthUrl, AdfsConfiguration.AdfsInstance, AdfsConfiguration.ClientId, AdfsConfiguration.Resource, AdfsConfiguration.UrlEncodedRedirectUri);
        var authCode = "";

        try
        {
            do
            {
                var result = await Client.GetAsync(authUrl);
                await result.Content.ReadAsStringAsync();
                IEnumerable<string> values;
                if (result.Headers.TryGetValues("location", out values))
                {
                    foreach (string s in values)
                    {
                        if (s.Contains("code="))
                        {
                            authUrl = "";
                            authCode = s.Substring(s.IndexOf("code=", StringComparison.Ordinal) + 5);
                        }
                        else
                        {
                            authUrl = s;
                        }
                    }
                }
                else
                {
                    authUrl = "";
                }
            } while (!string.IsNullOrEmpty(authUrl));

            return authCode;
        }
        catch (Exception exp)
        {
            var additionalInfo = $"additionalInfo : [authUrl: {authUrl}]";
            throw new SecurityException($"AdfsAuthorization.GetAuthCodeForWinAppUserAsync is failed, {additionalInfo}", exp);
        }

第 2 步:传递 Auth 代码以从 Adfs 服务器获取 jwt 令牌

        if (!AdfsConfiguration.IsInitialized) throw new SecurityException(Constants.AdfsConfigurationInitilizationExceptionMessage);

        var client = new WebClient();
        try
        {
            if (AdfsConfiguration.UseProxy == "Y")
            {
                var proxyObject = new WebProxy("Proxy", 80) { Credentials = CredentialCache.DefaultNetworkCredentials };
                client.Proxy = proxyObject;
            }

            //Uri address = new Uri(String.Format("https://{0}/adfs/oauth2/token/", AdfsInstance));
            Uri address = new Uri(string.Format(AdfsConfiguration.AdfsTokenServiceUrl, AdfsConfiguration.AdfsInstance));

            Uri redirectAddress = new Uri(AdfsConfiguration.RedirectUri);

            NameValueCollection values = new NameValueCollection
            {
                {"client_id", AdfsConfiguration.ClientId},
                {"grant_type", "authorization_code"},
                {"code", code},
                {"redirect_uri", redirectAddress.ToString()}
            };

            byte[] responseBytes = client.UploadValues(address, "POST", values);

            string response = System.Text.Encoding.UTF8.GetString(responseBytes);

            return response;

        }
        catch (Exception exp)
        {
            var additionalInfo = $" additionalInfo : [address: {string.Format(AdfsConfiguration.AdfsTokenServiceUrl, AdfsConfiguration.AdfsInstance) }, redirect Uri :{AdfsConfiguration.RedirectUri}]";
            throw new SecurityException($"AdfsAuthorization.GetAdfsOAuthTokenByAuthCode is failed, {additionalInfo}", exp);
        }
        finally
        {
            client.Dispose();
        }
于 2017-03-13T13:27:45.903 回答
0

获取应用程序池标识或登录用户的 SAML 断言:

        string rpLoginUrl = string.Format(SapConfiguration.AdfsSignInUrl, SapConfiguration.AdfsInstance, HttpUtility.UrlEncode(GetSapTokenServiceUrl));
        string htmlContent;

        try
        {
            do
            {
                var result = await Client.GetAsync(rpLoginUrl);
                htmlContent = await result.Content.ReadAsStringAsync();
                IEnumerable<string> values;
                if (result.Headers.TryGetValues("location", out values))
                {
                    foreach (string s in values)
                    {
                        if (s.StartsWith("/"))
                        {
                            rpLoginUrl = rpLoginUrl.Substring(0, rpLoginUrl.IndexOf("/adfs/ls", StringComparison.Ordinal)) + s;
                        }
                        else
                        {
                            rpLoginUrl = s;
                        }
                    }
                }
                else
                {
                    rpLoginUrl = "";
                }
            } while (!string.IsNullOrEmpty(rpLoginUrl));
        }
        catch (Exception exp)
        {
            var additionalInfo = $" additionalInfo : [rpLoginUrl: {rpLoginUrl}]";
            throw new SecurityException($"SapAuthorization.GetSamlResponseForProcessIdentityAsync is failed, {additionalInfo}", exp);
        }

        var reg = new Regex("SAMLResponse\\W+value\\=\\\"([^\\\"]+)\\\"");
        var matches = reg.Matches(htmlContent);
        string lastMatch = null;
        foreach (Match m in matches)
        {
            lastMatch = m.Groups[1].Value;
        }

        return lastMatch;
于 2017-03-13T13:38:42.170 回答