.NET 单点登录

 <appSettings>
    <!--是否启用单点登录接口-->
    <add key="IsStartCas" value="false"/>
    <!--Cas登录地址-->
    <add key="loginUrl" value=">
    <!--Cas验证地址-->
    <add key="validateUrl" value="
>
    <!--Cas注销地址-->
    <add key="logoutUrl" value="
>
  </appSettings>

using System;
using System.Web.Security;
using System.Web;
using System.Net;
using System.IO;
using System.Xml;
using System.Security.Principal;
using System.Configuration;
using System.Web.SessionState;

namespace LcSoftCard.CasModule
{
    /// <summary>
    /// Cas单点登录接口
    /// yisafe
    /// 2010-08-13
    /// </summary>
    public class SSOCasModule : IHttpModule, IReadOnlySessionState
    {
        //Return url coolie name
        protected const string ReturnUrl = "LcSoftCard.CasModule";
        public void Init(HttpApplication application)
        {
            string IsStartCas = ConfigurationManager.AppSettings.Get("IsStartCas");
            if (IsStartCas == "true")
            {
                application.AuthenticateRequest += (new EventHandler(this.Application_AuthenticateRequest));
                //application.PreRequestHandlerExecute += (this.context_PreRequesHandlerExecute);
            }
        }

        private void Application_AuthenticateRequest(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            string casLogin = ConfigurationManager.AppSettings.Get("loginUrl");
            string casValidate = ConfigurationManager.AppSettings.Get("validateUrl");
            if (casLogin == null || casLogin.Length < 1 || casValidate == null || casValidate.Length < 1)
            {
                // trigger a server error if cashost is not set in the web.config
                application.Response.StatusCode = 500;
                return;
            }
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = application.Request.Cookies[cookieName];
            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = null;
                try
                {
                    authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                }
                catch
                {
                    // TODO: Make a 500 error or go back to authentication
                    return;
                }

                if (authTicket == null)
                {
                    // TODO: Make a 500 error or go back to authentication
                    return;
                }
                // create an identity objet
                FormsIdentity identity = new FormsIdentity(authTicket);
                // create a principal
                GenericPrincipal principal = new GenericPrincipal(identity, null);
                // attach the principal to tue context objet that will flow throughout the request.
                application.Context.User = principal;
            }
            else
            {
                // Check if we are back from CAS Authentication
                // Look for the "ticket=" string after the "?" in the URL when back from CAS
                string casTicket = application.Request.QueryString["ticket"];
                // The CAS service name is the page URL for CAS Server call back
                // so any query string is discard.
                string service = application.Request.Url.GetLeftPart(UriPartial.Path);
                if (casTicket == null || casTicket.Length == 0)
                {
                    // memorize the initial request query string
                    application.Response.Cookies[ReturnUrl].Value = application.Request.RawUrl;
                    // redirect to cas server
                    string redir = casLogin + "?service=" + service;
                    application.Response.Redirect(redir);
                    return;
                }
                else
                {
                    // Second pass (return from CAS server) because there is a ticket in the query string to validate
                    string validateurl = casValidate + "?ticket=" + casTicket + "&" + "service=" + service;
                    WebClient client = new WebClient();
                    StreamReader Reader = new StreamReader(client.OpenRead(validateurl));

                    // Put the validation response in a string
                    string resp = Reader.ReadToEnd();

                    // Some boilerplate to set up the parse of validation response.
                    NameTable nt = new NameTable();
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
                    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
                    XmlTextReader reader = new XmlTextReader(resp, XmlNodeType.Element, context);

                    string netid = null;

                    // A very dumb use of XML by looping in all tags.
                    // Just scan for the "user". If it isn't there, its an error.
                    while (reader.Read())
                    {
                        if (reader.IsStartElement())
                        {
                            string tag = reader.LocalName;
                            if (tag == "user")
                            {
                                netid = reader.ReadString();
                                break;
                            }
                        }
                    }
                    reader.Close();

                    // If there was a problem, leave the message on the screen. Otherwise, return to original page.
                    if (netid == null)
                    {
                        application.Response.Write("Get Cas Configuration error!");
                    }
                    else
                    {
                        application.Response.Write("Bienvenue " + netid);
                        // create the authentication ticket and store the roles in the user data
                        LcSoftCard.BLL.TMasterFormsTicket bll = new LcSoftCard.BLL.TMasterFormsTicket();
                        LcSoftCard.Model.TMasterFormsTicket model = bll.GetModel(netid);
                        if (model.PersonId != Guid.Empty)
                        {
                            string UserData = model.PersonId + "|" + model.PersonNo + "|" + model.PersonName;
                            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.MasterId.ToString(), DateTime.Now, DateTime.Now.AddMinutes(LcSoftCard.Service.AppConfig.SessionTimeOut), true, UserData);
                            // encrypt the ticket
                            string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                            // create a cookie and use the encrypted ticket as data
                            authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                            // add the cookie to the response cookie collection
                            application.Response.Cookies.Add(authCookie);

                            go the initial request URL
                            //string returnUrl;
                            if the return url cookie is lost, return to the default page
                            //if (application.Request.Cookies[ReturnUrl] == null)
                            //    returnUrl = application.Request.ApplicationPath;
                            //else
                            //    returnUrl = application.Request.Cookies[ReturnUrl].Value;

                            application.Response.Redirect(FormsAuthentication.DefaultUrl);
                        }
                        else
                        {
                            application.Response.Write("Error:system not " + netid);
                        }
                    }
                }
            }
        }

        private void context_PreRequesHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            System.Web.HttpContext.Current.Session["PersonNo"] = application.Context.User.Identity.Name;
        }

        public void Dispose()
        {
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值