• 使用 HttpModel与现有基于共享登录信息( Cookie )的网站进行集成


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

  • 相关阅读:
    添加事件(jquery)
    闭包导致的问题
    学习之js绑定事件
    第二条 一个类如果有多个参数,考虑用Builder构造者模式
    用Intellij IDEA 创建第一个maven项目!
    OrderSessionHelper查看订单在session是否存在的辅助类
    css——overflow
    css——盒子
    css——外部样式
    css——权重叠加
  • 原文地址:https://www.cnblogs.com/shiningrise/p/2857067.html
Copyright © 2020-2023  润新知