首页 > 编程语言 >基于SAML2.0的SSO实现(C#&Java)

基于SAML2.0的SSO实现(C#&Java)

时间:2023-11-01 20:56:57浏览次数:31  
标签:Java string C# SAML2.0 #### var new samlResponse

背景

如题

原理

和对接其他平台一样,在开始之前我们要向调用的服务方申请一些东西,以便后面开展工作,比如,下图就是微信公众平台开发时候需要进行的操作:

我们的开发也是一样,也需要这样的前期准备。
这个过程可能是通过线上填写信息,也可能发个邮件加个附件,也可以是线下找对应的人员沟通。
总之,目的就是:交换信息。

交换信息的目的就是后面流程使用

准备

我所在的项目是通过发送XML文件给IdP的IT管理人员,长这样:

<?xml version="1.0" encoding="UTF-8"?>
<md:EntityDescriptor ID="" entityID="sp.cnblogs.com" xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
    <md:SPSSODescriptor AuthnRequestsSigned="false" ID="" WantAssertionsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
        <md:KeyDescriptor use="signing">
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <ds:X509Data>
                    <ds:X509Certificate>####</ds:X509Certificate>
                </ds:X509Data>
            </ds:KeyInfo>
        </md:KeyDescriptor>
        <md:KeyDescriptor use="encryption">
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <ds:X509Data>
                    <ds:X509Certificate>####</ds:X509Certificate>
                </ds:X509Data>
            </ds:KeyInfo>
        </md:KeyDescriptor>
        <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
                                     Location="https://sp.cnblogs.com/sso/acs"
                                     index="0" isDefault="true"/>
        <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
                                     Location="https://sp.cnblogs.com/sso/acs"
                                     index="1" isDefault="false"/>
    </md:SPSSODescriptor>
</md:EntityDescriptor>

然后对方给了我这样一个XML,这里面主要有IdP的Login URL和证书信息。

<md:EntityDescriptor ID="" cacheDuration="" entityID="sso.cnblogs.com"
    xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata">
    <md:IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"
        WantAuthnRequestsSigned="false">
        <md:KeyDescriptor use="signing">
            <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <ds:X509Data>
                    <ds:X509Certificate>####</ds:X509Certificate>
                </ds:X509Data>
            </ds:KeyInfo>
        </md:KeyDescriptor>
        <md:NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified</md:NameIDFormat>
        <md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
            Location="https://sso.cnblogs.com/idp/SSO.saml2" />
        <md:SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect"
            Location="https://sso.cnblogs.com/idp/SSO.saml2" />
    </md:IDPSSODescriptor>
    <md:ContactPerson contactType="administrative">
        <md:Company></md:Company>
        <md:GivenName></md:GivenName>
        <md:SurName></md:SurName>
        <md:EmailAddress></md:EmailAddress>
    </md:ContactPerson>
</md:EntityDescriptor>

ASP.NET MVC实现

  1. Install-Package AspNetSaml
    https://github.com/jitbit/AspNetSaml

  2. web.config

<appSettings>
  <add key="IdpLoginURL" value="https://sso.cnblogs.com/idp/SSO.saml2" />
  <add key="IdpCertificate" value="####" />
  <add key="SpEntityId" value="sp.cnblogs.com" />
  <add key="SpACSURL" value="https://sp.cnblogs.com/sso/acs" />
</appSettings>
  1. 登录逻辑
[HttpPost]
public ActionResult Login(string userName)
{
  if (NeedSSO())
  {
    var url = new AuthRequest(SpEntityId, SpACSURL).GetRedirectUrl(IdpLoginURL);
    return Redirect(url);
  }
}

  1. 回调逻辑
//this is https://sp.cnblogs.com/sso/acs
[HttpPost]
public ActionResult ACS()
{
    string responseString = Request.Form["SAMLResponse"];
    var samlResponse = new Response(IdpCertificate, responseString);
    if (samlResponse.IsValid())
    {
        var userName = samlResponse.GetNameID();
        //other logic
    }
    //other logic
}

完。

标签:Java,string,C#,SAML2.0,####,var,new,samlResponse
From: https://www.cnblogs.com/talentzemin/p/17804071.html

相关文章

  • Allwinner SoC based boards
    AllwinnerSoCbasedboardsForboardsusinganAllwinnerARMbasedSoC("sunxi"),theU-Bootbuildsystemgeneratesasingleintegratedimagefile: u-boot-sunxi-with-spl.bin. ThisfilecanbeusedonSDcards,eMMCdevices,SPIflashandforthe......
  • 实验3 C语言函数应用编程
    任务1源码1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#include<windows.h>5#defineN806voidprint_text(intline,intcol,chartext[]);//函数声明7voidprint_spaces(intn);//函数声明8voidprint_bl......
  • desertification
    1、In order to prevent the further aggravation of desertification, the international community has taken a series of prevention and control measures. These include land management and conservation regulations, the promotion of s......
  • CF911G Mass Change Queries
    CF911GMassChangeQueries题解首先这题有一个很一眼的分块做法,并且由于只需要维护颜色,所以会极其好写。对每个块维护并查集,表示整块中颜色变成了哪个颜色,每个位置单独也指向一个颜色表示最初指向哪个颜色,这样就很好维护了。但是发现值最大只有\(100\),所以考虑和值相关的做......
  • Chrome字体插件
    FontsChanger字体插件可以强制修改字体,但是一些等宽字体被替换后效果不好,可手动修改代码background.jsconstgetFont=()=>newPromise(ok=>chrome.fontSettings.getFont({genericFamily:"standard"},({fontId})=>ok(fontId)));constgetFontFix=()=>newPro......
  • 城市时空预测的统一数据管理和综合性能评估 [实验、分析和基准]《Unified Data Manage
    2023年11月1日,还有两个月,2023年就要结束了,希望在结束之前我能有所收获和进步,冲呀,老咸鱼。 摘要解决了访问和利用不同来源、不同格式存储的不同城市时空数据集,以及确定有效的模型结构和组件。1.为城市时空大数据设计的统一存储格式“原子文件”,并在40个不同的数据集上验证了其......
  • NOIP2023模拟8联测29 C. 蛋糕
    NOIP2023模拟8联测29C.蛋糕目录NOIP2023模拟8联测29C.蛋糕题目大意思路code题目大意你现在得到了一个二维蛋糕,它从左到右可以分成\(n\)列,每列高为\(a_i\)。对于每一列,又可以从下到上分为\(a_i\)块,并且最上面一块权值为\(1\),从上到下权值依次加。每一列的最上面的......
  • for in /continue/ break
    1.forinfor(变量in对象){代码段}for...in...语句是用来遍历对象种的每个属性,每次都能将属性名作为字符串保存在变量里在无非预对象的任何信息,和循环次数的时候使用vararr=[{'name':"jake",'age':18,......
  • 高斯数据库HCNA之数据库技术概述
    一、数据库技术概述数据库从诞生之日至今已经60年,从早起单纯的对数据文件的保存和处理,发展出以数据建模和数据库管理系统核心技术为主的一门内容丰富的学科,成为现代计算机应用系统的基础和核心,伴随着互联网、大数据、人工智能等技术的蓬勃兴起,数据库技术和产品更是百花齐放,带动了......
  • TCP三次握手与四次挥手
    一、TCP三次握手过程?​ TCP是面向连接的协议,所以使用TCP前必须先建立连接,而建立连接是通过三次握手来进行的。三次握手的过程如下图:一开始,客户端和服务端都处于CLOSE状态。先是服务端主动监听某个端口,处于LISTEN状态客户端会随机初始化序号(client_isn),将此序号置于......