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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/****************************************************************************
 * 功能说明:
 *  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;
        }
    }
}<br><br>
posted @   shiningrise  阅读(378)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2007-01-11 测试一下我的新技术博客
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css
点击右上角即可分享
微信分享提示