使用 SAML 和 Azure AD 设置 SSI 的最佳方式
Best way to set up SSI with SAML and Azure AD
根据这篇文章,我的印象是您可以使用 SAML 标准在使用 Azure AD 的 Web 应用程序上对用户进行身份验证:
https://docs.microsoft.com/en-us/azure/active-directory/develop/single-sign-on-saml-protocol
然而,对于执行此操作所需的所有小步骤并不完全清楚。所以我做了以下事情来为自己弄清楚第一步是获取 URL 服务提供商需要重定向浏览器以使用 IdP(即 Azure AD)进行身份验证:
- 我在 Azure 中的免费套餐中设置了一个单独的 AD。
- 在该 AD 中,我创建了一个应用程序。
- 应用程序中没有设置 SSI 的选项
然后我使用以下代码为 SAML 浏览器请求生成 URL:
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace AzureSAMLExperiment
{
class Program
{
// Call to https://login.microsoftonline.com/common/FederationMetadata/2007-06/FederationMetadata.xml returns the SingleSignOnService element below:
public const string SingleSignOnServiceUrl = "https://login.microsoftonline.com/common/saml2";
public const string SingleSignOnQueryString = "?SAMLRequest={0}";
static void Main(string[] args)
{
// See https://docs.microsoft.com/en-us/azure/active-directory/develop/single-sign-on-saml-protocol
var SAMLRequestXML = $@"<samlp:AuthnRequest
xmlns=""urn:oasis:names:tc:SAML:2.0:assertion""
ID=""id6c1c178c166d486687be4aaf5e482730""
Version=""2.0"" IssueInstant=""{DateTime.UtcNow.ToString("o")}""
xmlns:samlp=""urn:oasis:names:tc:SAML:2.0:protocol"">
<Issuer xmlns=""urn:oasis:names:tc:SAML:2.0:assertion"">ISSUER</Issuer>
</samlp:AuthnRequest>";
var url = $"{SingleSignOnServiceUrl}?SAMLRequest={DeflateEncode(SAMLRequestXML)}";
}
private static string DeflateEncode(string val)
{
var memoryStream = new MemoryStream();
using (var writer = new StreamWriter(new DeflateStream(memoryStream, CompressionMode.Compress, true), new UTF8Encoding(false)))
{
writer.Write(val);
writer.Close();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None);
}
}
}
}
并将结果 URL 放入浏览器。
我为 ISSUER 尝试了几个不同的值,包括
- http://localhost
- 我们公司的域名
- https://sts.windows.net/{租户指南}
- {租户指南}
- {应用程序 guid}
但是 none 有效。
如您所见,我使用的是通用 URL 而不是租户特定的。我不确定哪个最好用。
每次访问 url 时,我都会收到以下回复:
关于我做错了什么,有什么指示吗?
- 我需要高级广告还是免费的好吗?
- 我应该使用租户还是公共端点?
- XML或者我使用的编码有问题吗?
Azure AD 免费提供活动目录服务。要通过 SAML 协议连接到活动目录,您需要切换到付费计划。使用付费计划并配置 SAML 设置后,Azure 将充当 SAML IdP(身份提供者)。此时,我建议使用配置为 SAML SP(服务提供商)的外部 IAM 服务来测试身份验证流程,而不是自行制定解决方案。例如,您可以为此尝试 Auth0 as SAML SP。
使用上述设置,架构会是这样的;
Your App <= OAuth => Auth0 <= SAML => Azure AD
如果您不想为 SAML 支持向 Azure 付费,您可以使用 WsFed 协议将用户联合到 Azure AD。这也是supported with Auth0.
Your App <= OAuth => Auth0 <= WsFed => Azure AD
This link 可能对 .Net 的某些链接很有用,如果您需要在您的应用程序中支持 SAML 协议,并且还提供更多链接以获得更广阔的视野的可能性。
免责声明:我为 Auth0 工作。
是的,你需要 Azure AD Premium。
这是一个使用自定义 SAML 连接的 example。
然后是 Azure AD/企业应用程序/SSO。
根据这篇文章,我的印象是您可以使用 SAML 标准在使用 Azure AD 的 Web 应用程序上对用户进行身份验证:
https://docs.microsoft.com/en-us/azure/active-directory/develop/single-sign-on-saml-protocol
然而,对于执行此操作所需的所有小步骤并不完全清楚。所以我做了以下事情来为自己弄清楚第一步是获取 URL 服务提供商需要重定向浏览器以使用 IdP(即 Azure AD)进行身份验证:
- 我在 Azure 中的免费套餐中设置了一个单独的 AD。
- 在该 AD 中,我创建了一个应用程序。
- 应用程序中没有设置 SSI 的选项
然后我使用以下代码为 SAML 浏览器请求生成 URL:
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
namespace AzureSAMLExperiment
{
class Program
{
// Call to https://login.microsoftonline.com/common/FederationMetadata/2007-06/FederationMetadata.xml returns the SingleSignOnService element below:
public const string SingleSignOnServiceUrl = "https://login.microsoftonline.com/common/saml2";
public const string SingleSignOnQueryString = "?SAMLRequest={0}";
static void Main(string[] args)
{
// See https://docs.microsoft.com/en-us/azure/active-directory/develop/single-sign-on-saml-protocol
var SAMLRequestXML = $@"<samlp:AuthnRequest
xmlns=""urn:oasis:names:tc:SAML:2.0:assertion""
ID=""id6c1c178c166d486687be4aaf5e482730""
Version=""2.0"" IssueInstant=""{DateTime.UtcNow.ToString("o")}""
xmlns:samlp=""urn:oasis:names:tc:SAML:2.0:protocol"">
<Issuer xmlns=""urn:oasis:names:tc:SAML:2.0:assertion"">ISSUER</Issuer>
</samlp:AuthnRequest>";
var url = $"{SingleSignOnServiceUrl}?SAMLRequest={DeflateEncode(SAMLRequestXML)}";
}
private static string DeflateEncode(string val)
{
var memoryStream = new MemoryStream();
using (var writer = new StreamWriter(new DeflateStream(memoryStream, CompressionMode.Compress, true), new UTF8Encoding(false)))
{
writer.Write(val);
writer.Close();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None);
}
}
}
}
并将结果 URL 放入浏览器。
我为 ISSUER 尝试了几个不同的值,包括
- http://localhost
- 我们公司的域名
- https://sts.windows.net/{租户指南}
- {租户指南}
- {应用程序 guid}
但是 none 有效。
如您所见,我使用的是通用 URL 而不是租户特定的。我不确定哪个最好用。
每次访问 url 时,我都会收到以下回复:
关于我做错了什么,有什么指示吗?
- 我需要高级广告还是免费的好吗?
- 我应该使用租户还是公共端点?
- XML或者我使用的编码有问题吗?
Azure AD 免费提供活动目录服务。要通过 SAML 协议连接到活动目录,您需要切换到付费计划。使用付费计划并配置 SAML 设置后,Azure 将充当 SAML IdP(身份提供者)。此时,我建议使用配置为 SAML SP(服务提供商)的外部 IAM 服务来测试身份验证流程,而不是自行制定解决方案。例如,您可以为此尝试 Auth0 as SAML SP。
使用上述设置,架构会是这样的;
Your App <= OAuth => Auth0 <= SAML => Azure AD
如果您不想为 SAML 支持向 Azure 付费,您可以使用 WsFed 协议将用户联合到 Azure AD。这也是supported with Auth0.
Your App <= OAuth => Auth0 <= WsFed => Azure AD
This link 可能对 .Net 的某些链接很有用,如果您需要在您的应用程序中支持 SAML 协议,并且还提供更多链接以获得更广阔的视野的可能性。
免责声明:我为 Auth0 工作。
是的,你需要 Azure AD Premium。
这是一个使用自定义 SAML 连接的 example。
然后是 Azure AD/企业应用程序/SSO。