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

打印 上一主题 下一主题

主题 846|帖子 846|积分 2540

最近公司的很多项目都要改单点登录了,不外大部分都还没敲定,目前立刻要做的就只有一个比力老的项目
先改一个试试手,主要目标就是最短最快实现功能
起首因为要保留原登录方式,所以页面上的改动就是在原来登录页面下加一个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/fd799da1-bfc1-4234-a91c-72b3a1cb9e26/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()验证过就说明单点登录已经完成 
内里的部分就是承接原系统的正常登录逻辑,仅供参考,具体按照系统的登录调解
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

拉不拉稀肚拉稀

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表