c#实现单点登陆

本文介绍了如何使用C#实现单点登录(SSO)系统。通过接收客户端传递的key值,系统向客户的ESB系统验证,验证成功后获取用户信息并与本地服务器用户信息对比。在客户内网环境且域名受限的情况下,需进行远程调试,并利用SOAP协议生成WSDL文件的代理类完成接口调用。
摘要由CSDN通过智能技术生成

原理:客户访问我的超链接,传来一个key值,我通过这个key‘值去客户的esb系统去验证,若验证成功,返回用户信息,我拿到用户信息,跟本地服务器的用户信息进行比对,若一致,跳过登陆页面,直接授权进入系统。客户是内网,域名又有限制,访问不到接口,只能进行远程调试;因为获取用户用的是soap协议,所以还需要生成wsdl文件的代理类。

主要代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Security.Cryptography;
using TechExcel.Project.DataAccess;
using TechExcel.Project.DataAccess.Service;
using TechExcel.Project.DataAccess.Utility;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.IO;
using System.Xml;
using TechExcel.PPM.ProjectWeb.WebServiceEastHope;

namespace TechExcel.PPM.ProjectWeb
{
    public partial class SSOLogin4EastHope : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CookieContainer cookieContainer = new CookieContainer();
            string url = ConfigurationManager.AppSettings.Get("EastHopeEsbUrl");//esb接口的url
            string strUser= string.Empty;//用户信息
            string token = string.Empty;//key值
            string iPlanetDirectoryProVal = string.Empty;//
            int timeout = 0;
            string userAgent = string.Empty;
            string strLang = string.Empty;
            int nLangID = 0;
            int.TryParse(strLang, out nLangID);
            string strRuntimeKey = string.Empty;         
            if (Request.Cookies == null)
            {
                Response.Write("cookie is null");
                //Response.Redirect("login.aspx");
                return;
            }
            if (HttpContext.Current.Request.Cookies["tokenID"] != null)
            {
                iPlanetDirectoryProVal = HttpContext.Current.Request.Cookies["tokenID"].Value;
            }
            if (HttpContext.Current.Request.Cookies["iPlanetDirectoryPro"] != null)
            {
              
                string tokenTemp = HttpContext.Current.Request.Cookies["iPlanetDirectoryPro"].Value;
                Response.Write("iPlanetDirectoryPro is not null " + tokenTemp);
                string xmlToken=GetXml(tokenTemp);
                //拿到cookies的key值并且生成xml数据的格式之后,到ESB系统获取用户信息
                //Cookie cookie = new Cookie("tokenID", tokenTemp);
                //HttpHelper httpR = new HttpHelper();
                //HttpWebResponse re = httpR.CreateGetHttpResponse(url, timeout, userAgent, cookie,xmlToken);
                AuthenticationWSClient authticayion=new AuthenticationWSClient();
                string xmlIn=authticayion.getTokenUser(xmlToken);
                if (xmlIn != null)
                {
                    //string xmlIn = new StreamReader(re.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                   
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单点登录(Single Sign-On,SSO)是指用户只需登录一次,即可在多个应用系统中访问被授权的资源,而无需再次登录或提供身份验证信息。在 C# 中,可以通过以下步骤实现单点登录: 1. 配置身份认证和授权。 在 Web.config 文件中,配置身份认证和授权,例如: ``` <system.web> <authentication mode="Forms"> <forms name=".ASPXAUTH" loginUrl="~/Account/Login" timeout="2880" /> </authentication> <authorization> <deny users="?" /> </authorization> </system.web> ``` 2. 实现登录页面。 在登录页面中,用户输入用户名和密码,然后将其传递到服务器进行验证。如果验证通过,则使用 FormsAuthentication.SetAuthCookie 方法创建认证票证,并跳转到主页。 ``` if (IsValid(username, password)) { FormsAuthentication.SetAuthCookie(username, false); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", "The user name or password is incorrect."); return View(); } ``` 3. 在其他应用系统中验证认证票证。 其他应用系统可以通过检查认证票证来验证用户的身份。可以使用 FormsAuthentication.Decrypt 方法解密认证票证,然后检查票证是否过期或是否被篡改。 ``` HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); if (authTicket != null && !authTicket.Expired && authTicket.Name == username) { // User is authenticated } } ``` 以上是单点登录的基本实现步骤,但具体实现还需要根据实际需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值