ASP.NETForums與現有系統整合方案示例
注意我在所有的地方都catch吸收掉錯誤,而無返回,你不一定要這么做。
| public void Register() { try { User user = new User(); user.Username = Username; user.Password = Password; user.Email = "[email protected]"; user.AccountStatus = UserAccountStatus.Approved; user.IsAnonymous = false; Users.Create(user, false); } catch { } } |
登錄
基本上都是復制粘貼過來的,注意最后的setcookie,這是同步登錄的關鍵。
| public void Login() { try { User userToLogin = new User(); userToLogin.Username = Username; userToLogin.Password = Password; LoginUserStatus loginStatus = Users.ValidUser(userToLogin); 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) { return; } } set_Cookie(userToLogin.Username, "1"); } } catch { } } |
修改密碼
這個假設是基于登錄已經發生的,如果你的假設不是這樣需要加入自己的判斷。
| public void ChangePassword() { try { ForumContext forumContext = ForumContext.Current; User user = forumContext.User; if (user != null) { user.ChangePassword(Password, NewPassword); } } catch { } } |
設置cookie
| public void set_Cookie(string Username, string Selet_item) { if(Selet_item == "0") { FormsAuthentication.SetAuthCookie(Username,false); } else { ForumContext forumContext = ForumContext.Current; FormsAuthentication.SetAuthCookie(Username,true); forumContext.Context.Response.Cookies[FormsAuthentication.FormsCookieName].Expires=DateTime. _Now.AddDays(System.Convert.ToInt32(Selet_item)); } } |
最后在已有系統的注冊,登錄,修改的最后Response.Redirect這頁就ok,不喜歡Redirect,Server.Transfer,xmlhttp都行,看你需要的展現了。還有一點很關鍵,就是要加密了你傳輸的字符串,要不明文就……※¥※×%※× ,要是嫌還不安全就用ip判斷一下誰可以訪問這頁!