翼度科技»论坛 编程开发 .net 查看内容

.NET Framework 旧系统新增SSO单点登录实例

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
最近公司的很多项目都要改单点登录了,不过大部分都还没敲定,目前立刻要做的就只有一个比较老的项目
先改一个试试手,主要目标就是最短最快实现功能
首先因为要保留原登录方式,所以页面上的改动就是在原来登录页面下加一个SSO登录入口
用超链接写的入口,页面改造后如下图:

其中超链接的 href="StaffLogin.aspx"  
新建Web窗体StaffLogin.aspx(名字可以随便起,只要能对应的上,不必非要叫StaffLogin),
前台不用管,直接写对应的后台StaffLogin.aspx.cs
注意添加引用:using Saml;
 具体代码如下:
  1. [/code][code]protected void Page_Load(object sender, EventArgs e)
  2. {
  3.             if (!IsPostBack)
  4.             {
  5.                 var samlEndpoint = "https://login.microsoftonline.com/fdaaaaa1-bfc1-4234-a91c-72bbbbbb9e26/saml2";
  6.                 var request = new AuthRequest(
  7.                     "aaaprd", //TODO: put your app's "entity ID" here
  8.                     "https://xxxx此处替换为网址xxxx.com/Auth" //TODO: put Assertion Consumer URL (where the provider should redirect users after authenticating)
  9.                 );
  10.                 //now send the user to the SAML provider
  11.                 string redirturl = request.GetRedirectUrl(samlEndpoint);
  12.                 Response.Write("");
  13.             }
  14. }
复制代码
request里面两个参数,第一个是entityID(是系统的唯一标识)  第二个是单点登录验证通过后回调的url
新建Web窗体Auth.aspx(名字可以随便起,只要能对应的上,不必非要叫Auth,这里叫这个名字是因为跟对接的人约定的回调URL是这个),
前台不用管,直接写对应的后台Auth.aspx.cs
具体代码如下:
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3.             if (!IsPostBack)
  4.             {
  5.                 try
  6.                 {if (Request.Form.HasKeys() && null != Request.Form["SAMLResponse"])
  7.                     {
  8.                         string samlCertificate = @"-----BEGIN CERTIFICATE-----
  9.                         此处为证书编码
  10.                         -----END CERTIFICATE-----";<br>                        var samlResponse = new Response(samlCertificate, Request.Form["SAMLResponse"]);<br>                        if (samlResponse.IsValid())
  11.                         {
  12.                             var mail = samlResponse.GetNameID(); //let's get the username
  13.                             DFS_S_UserInfo modelUser = new DFS_S_UserInfoBll().GetModelByMail(mail);
  14.                             if (modelUser == null)
  15.                             {string redirectUrl = string.Format("~/login.aspx?errorMessage={0}", HttpUtility.UrlEncode("系统内用户不存在"));
  16.                                 Response.Redirect(redirectUrl);
  17.                             }
  18.                             else
  19.                             {
  20.                                 LoginInfo.SetInfo(modelUser.UserID);
  21.                                 if (LoginInfo.GetCurrentLoginInfo() != null)
  22.                                 {if (LoginInfo.GetCurrentLoginInfo().UserID > 0)
  23.                                     {
  24.                                         Response.Redirect("~/home.aspx", false); // 注意:endResponse设置为false  
  25.                                         Context.ApplicationInstance.CompleteRequest(); // 手动结束请求
  26.                                     }
  27.                                 }
  28.                                 else
  29.                                 {string redirectUrl = string.Format("~/login.aspx?errorMessage={0}", HttpUtility.UrlEncode("人员登录验证失败"));
  30.                                     Response.Redirect(redirectUrl);
  31.                                 }
  32.                             }
  33.                         }
  34.                         else
  35.                         {string redirectUrl = string.Format("~/login.aspx?errorMessage={0}", HttpUtility.UrlEncode("人员登录验证失败"));
  36.                             Response.Redirect(redirectUrl);
  37.                         }
  38.                     }
  39.                     else
  40.                     {string redirectUrl = string.Format("~/login.aspx?errorMessage={0}", HttpUtility.UrlEncode("人员登录验证失败"));
  41.                         Response.Redirect(redirectUrl);
  42.                     }
  43.                 }
  44.                 catch(Exception ex)
  45.                 {
  46.                     string redirectUrl = string.Format("~/login.aspx?errorMessage={0}", HttpUtility.UrlEncode("系统错误,请联系IT"));
  47.                     Response.Redirect(redirectUrl);
  48.                 }
  49.                
  50.             }
  51.         }
复制代码
上述代码里证书samlCertificate是客户方提供的,entityID也是在拿到证书前先提供过去的
实际开发中,肯定要有的,这个不用担心,拿到证书替换进去既可,具体代码结构跟我这个应该差不多
samlResponse.IsValid()验证过就说明单点登录已经完成 
里面的部分就是承接原系统的正常登录逻辑,仅供参考,具体按照系统的登录调整
 

来源:https://www.cnblogs.com/fkcxy/p/18242504
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具