ComponentSpace SAML v2.0 for .NET 使用介绍

时间:2023-03-09 19:42:07
ComponentSpace SAML v2.0 for .NET 使用介绍

下载地址:http://samlsso.codeplex.com/

以下描叙参考版本为其官网最新版本2.5.0.6。相对2.4版本,2.5有了很大改进,很多接口方法都变了。使用起来更方便,易懂。

广告:

使用简便,文档实例丰富。
1,基础知识
SSO,SAML.自行google。

2,使用步骤:

a,安装,安装完毕后在安装目录 ComponentSpace SAML v2.0 for .NET下有各个版本的解决方案。你也可以移步到Examples\SSO\HighLevelAPI。在移步的过程中,你会发现其他文件夹,你可以先去看那些文件也可以后面再看。以下使用步骤参考了Documentation\SAML v2.0 Developer Guide.pdf

b,导入证书。我打开的是2012版本。展开ExampleIdentityProvider和ExampleServiceProvider工程。导入idp.pfx和sp.cer。其余证书忽略。

打开两个工程下的saml.config和web.config,浏览下,可能对一些疑问会有所帮助。关于本步骤的证书导入,你就会发现在saml.config中有一段如下配置

<IdentityProvider Name="urn:componentspace:ExampleIdentityProvider"
CertificateFile="idp.pfx"
CertificatePassword="password"/>

c,发布站点

编译这两个工程,可以通过发布程序直接建立应用;也可以选择发布到一个目录,然后也在iis新建两个应用程序,应用程序路径指向发布的目录。注意的是应用程序名称同工程名称。应用程序先放在默认站点下,一半是default下。到了实际应用的时候,这些名称,站点都是可以改的,默认配置在saml.config下:

<PartnerServiceProvider Name="urn:componentspace:ExampleServiceProvider"
WantAuthnRequestSigned="false"
SignSAMLResponse="true"
SignAssertion="false"
EncryptAssertion="false"
AssertionConsumerServiceUrl="http://localhost/ExampleServiceProvider/SAML/AssertionConsumerService.aspx"
SingleLogoutServiceUrl="http://localhost/ExampleServiceProvider/SAML/SLOService.aspx"
CertificateFile="sp.cer"/>

<PartnerIdentityProvider Name="urn:componentspace:ExampleIdentityProvider"
SignAuthnRequest="false"
WantSAMLResponseSigned="true"
WantAssertionSigned="false"
WantAssertionEncrypted="false"
SingleSignOnServiceUrl="http://localhost/ExampleIdentityProvider/SAML/SSOService.aspx"
SingleLogoutServiceUrl="http://localhost/ExampleIdentityProvider/SAML/SLOService.aspx"
CertificateFile="idp.cer"/>

d,运行。ExampleServiceProvider和ExampleIdentityProvider和两个的default.aspx顺便你先浏览那个,第一次都会先到ExampleIdentityProvider要求输入密码,密码为password,保存在ExampleIdentityProvider的web.config中:

<authentication mode="Forms">
<forms name="ExampleIdentityProvider" loginUrl="login.aspx">
<credentials passwordFormat="Clear">
<user name="idp-user" password="password"/>
</credentials>
</forms>
</authentication>

3,总结

是不是很简单,直接把demo拿到项目中用,只需要从安装证书开始,替换成自己的证书,自己的站点。把authentication换成自己的验证。authentication这一段在ExampleIdentityProvider下的login页面里:

if (FormsAuthentication.Authenticate(userNameTextBox.Text, passwordTextBox.Text)) {
FormsAuthentication.RedirectFromLoginPage(userNameTextBox.Text, false);
} else {
errorMessageLabel.Text = "Invalid credentials. The user name and password should be \"idp-user\" and \"password\".";
}

本文旨在做个推介,其余logout等等可以参看guid文档。还要啰嗦一句的是,当初我还关注怎么从认证中心传值到本地,看看ExampleIdentityProvider下的saml/SSOService.aspx.cs

IDictionary<string, string> attributes = new Dictionary<string, string>();

foreach (string key in WebConfigurationManager.AppSettings.Keys) {
if (key.StartsWith(AppSettings.Attribute)) {
attributes[key.Substring(AppSettings.Attribute.Length + 1)] = WebConfigurationManager.AppSettings[key];
}
}

SAMLIdentityProvider.SendSSO(Response, userName, attributes);

以及ExampleServiceProvider下的saml/AssertionConsumerService.aspx.cs:

SAMLServiceProvider.ReceiveSSO(Request, out isInResponseTo, out partnerIdP, out userName, out attributes, out targetUrl);

可以发现,容器就是attributes。

至于MVC在同个目录有example。大致结构原理一样。

搭建一个SSO就是这么简单。从此PM再也不担心你的学习了。