使用 HttpModel與現有基於共用登入資訊( Cookie )的網站進行整合

來源:互聯網
上載者:User
/**************************************************************************** * 功能說明: *  1、使用 HttpModel與現有基於共用登入資訊( Cookie )的網站進行整合 *  2、使用者自動登入,自動註冊,,延時註冊, *  3、同步退出 *  * 使用方法: *  見樣本web.config *  *  設計編碼:shiningrise@gmail.com *  ****************************************************************************/using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.Security;using wojilu.Open;namespace wojilu.AuthenticationModule{    public class AuthenticationModule : System.Web.IHttpModule    {        public void Dispose()        {            //         throw new NotImplementedException();        }        public void Init(HttpApplication context)        {            context.BeginRequest += new EventHandler(Application_BeginRequest);            context.EndRequest += new EventHandler(Application_EndRequest);            context.AuthenticateRequest += new EventHandler(context_AuthenticateRequest);        }        void context_AuthenticateRequest(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;            HttpContext context = application.Context;            HttpRequest req = context.Request;            //定義起來測試用            HttpResponse response = context.Response;            OpenService openService = new OpenService();            if (req.IsAuthenticated)            {                if (openService.UserIsLogin(context) != true)                {                    string username = context.User.Identity.Name;                    //        response.Write("username=" + username);                    if (openService.UserIsRegister(username, context))                    {                        openService.UserLogin(username, context);                        this.UpdateUserProfile(username);                    }                    else                    {                        string userPwd = GetRandomNumberString(16);                        string userEmail = string.Format("{0}@lcsyzx.cn", username);                        openService.UserRegister(username, userPwd, userEmail, null, "home,blog,photo,microblog,friend,visitor,forumpost,about,feedback,share");                    }                }            }            else            {                //       response.Write(" IsAuthenticated = false ");                openService.UserLogout(context);            }        }        public void Application_BeginRequest(object sender, EventArgs e)        {        }        public void Application_EndRequest(object sender, EventArgs e)        {            //HttpApplication application = sender as HttpApplication;            //HttpContext context = application.Context;            //HttpResponse response = context.Response;            //    response.Write("這是來自自訂HttpModule中有EndRequest");        }        /// <summary>        /// 更新 Profile 的真實名到 wojilu        /// </summary>        /// <param name="usr"></param>        private void UpdateUserProfile(string username)        {            UserProfile userProfile = new UserProfile();//(UserProfile)ProfileBase.Create(usr.UserName, true);            userProfile.Initialize(username, true);            wojilu.Members.Users.Domain.User usr = new OpenService().getUserByName(username);            //UserName  LoginUserName  FullName  UserGroup  CurJi  CurBh  CurBnbh            if (userProfile.UserGroup == "學生")                usr.RealName = string.Format("{0}({1})班{2}", userProfile.CurJi, userProfile.CurBh, userProfile.FullName);            else if (userProfile.UserGroup == "教師")                usr.RealName = userProfile.FullName;            else                usr.RealName = string.Format("{0}{1}", userProfile.UserGroup, userProfile.FullName);            usr.Pwd = GetRandomNumberString(16);            if (usr.Name != "admin")                usr.update();            //    db.update(usr, "RealName");            //    db.update(usr, "Pwd");        }        /// <summary>        /// 產生隨機數字字串        /// </summary>        /// <param name="int_NumberLength">數字長度</param>        /// <returns></returns>        private string GetRandomNumberString(int int_NumberLength)        {            bool onlyNumber = false;            Random random = new Random();            string strings = "123456789";            if (!onlyNumber) strings += "abcdefghjkmnpqrstuvwxyz";            char[] chars = strings.ToCharArray();            string returnCode = string.Empty;            for (int i = 0; i < int_NumberLength; i++)                returnCode += chars[random.Next(0, chars.Length)].ToString();            return returnCode;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.