解决 Asp.Net Forums 与现有Web系统 统一登录的方法(:--))

今天一哥们要在系统当中挂接Asp.Net Forums论坛 ,但不想又在系统之外再进行一次论坛的登录,可以算得上一个单点登录的需求吧。花费半日终于搞定,希望能对同样要求的朋友有所帮助。

现就
[2005/04/13]
===================================================
Asp.Net Forums V2.2.1929 官方中文版
===================================================
的源码进行如下修改:
1)http://localhost/Forums/Themes/default/Skins/View-ForumGroupView.ascx处的
 Users.AutoLogin(); //本处为新增代码
if ( Users.GetUser().IsAnonymous ) 原来的可能处于第10行

2)C:\Inetpub\wwwroot\Forum\Components\Users.cs
新增如下代码
  #region bigmouthz@163.net 2006.04.17 interface for webpage call

  public static void AutoLogin()
  {
   ForumContext forumContext = ForumContext.Current;
   if (forumContext.InterFace_user != null )
   {
    User userToLogin = new User();
    userToLogin.Username = forumContext.InterFace_user;
    userToLogin.Password = forumContext.InterFace_password;
    userToLogin.IPLastLogin = Globals.IPAddress;
    userToLogin.IPLocation = AspNetForums.Components.IPScanner.IPLocation(Globals.IPAddress);
    userToLogin.Platform = AspNetForums.Users.GetUsersInfo(forumContext.Context.Request.UserAgent,1);
    userToLogin.Browser = AspNetForums.Users.GetUsersInfo(forumContext.Context.Request.UserAgent,2);
    LoginUserStatus loginStatus = Users.ValidUser(userToLogin,false);
    if( loginStatus == LoginUserStatus.Success )
    {
     // 如果系统设置不允许登录
     if (!Globals.GetSiteSettings().AllowLogin)
     {
      bool allowed = false;
      int userid = Users.FindUserByUsername( userToLogin.Username ).UserID;
      ArrayList roles = Roles.GetRoles(userid);
      // 如果是管理员,则设置允许登录
      foreach (Role role in roles)
      {
       if (role.Name == "Site Administrators" || role.Name == "Global Administrators")
       {
        allowed = true;
        break;
       }
      }
      // 处理用户登录处理
      if (!allowed)
      {
       FormsAuthentication.SetAuthCookie(userToLogin.Username,false);
      }
     }
              
     FormsAuthentication.SetAuthCookie(userToLogin.Username,true);
     forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime.Now.AddDays(30);
    }
   }
  
  }
 #endregion

3)C:\Inetpub\wwwroot\Forum\Components\Components\ForumContext.cs
中新增如下内容
  #region bigmouthz@163.net 2006.04.17 interface for webpage call
  string interface_user = "";
  string interface_password = "";
  public string InterFace_user { get { return interface_user; } set { interface_user = value; } }
  public string InterFace_password { get { return interface_password; } set { interface_password = value; } }
  #endregion

以及在方法当中       public ForumContext()
  {          
           context = HttpContext.Current;

            if (context == null)
                return;

//新增内容


   #region bigmouthz@163.net 2006.04.17 interface for webpage call
   interface_user = context.Request.QueryString["iuser"];
   interface_password = context.Request.QueryString["ipassword"];
   #endregion

 
..
}

4)另外要做到你本身系统 与Asp.Net Forums 用户密码表的内容同步 这个要注意及参考的地方代码在
Components下 的users.cs类 的  public static LoginUserStatus ValidUser(User user,bool isRequestFromWebService)
 
  这个呵呵 很好搞定的

5)调用系统当中的处理 方法
http://localhost/Forums/default.aspx?iuser=admin&ipassword=admin
只要注意将iuser 以及ipassword的传入值换成系统当前用户的名称以及密码就可以了


:-)

posted @ 2006-04-17 19:16  我想去长安  阅读(1698)  评论(3编辑  收藏  举报