/**************************************************************************** * 功能說明: * 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; } }}