温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:SpaceBuilder v1.1源代码
当前文件:
SpaceBuider11/BasicWebControls/Manage/AdminLogin.cs,打开代码结构图
SpaceBuider11/BasicWebControls/Manage/AdminLogin.cs,打开代码结构图1//------------------------------------------------------------------------------ 2
// <copyright company="Tunynet"> 3
// Copyright (c) Tunynet Inc. All rights reserved. 4
// </copyright> 5
//------------------------------------------------------------------------------ 6
7
using System; 8
using System.Collections.Generic; 9
using System.Text; 10
using SpaceBuilder.Controls.Utils; 11
using System.Web.UI.WebControls; 12
using SpaceBuilder.Components; 13
using System.Web; 14
using TunyNet.Security; 15
using SpaceBuilder.Configuration; 16
using System.Web.Security; 17
18
namespace SpaceBuilder.Web.Manage.Controls 19
{ 20
/// <summary> 21
/// 后台管理员用户登录 22
/// </summary> 23
public class AdminLogin : ManageBaseControl 24
{ 25
/// <summary> 26
/// 验证当前用户是否有此页面的访问权限 27
/// </summary> 28
protected override void Authorize() 29
{ 30
//登录页面不验证权限 31
} 32
33
protected override void OnInit(EventArgs e) 34
{ 35
if (SkinName == null) 36
SkinName = "Skin-AdminLogin.ascx"; 37
38
base.OnInit(e); 39
} 40
41
Childer Controls 79
/// <summary> 80
/// 附加子控件 81
/// </summary> 82
protected override void AttachChildControls() 83
{ 84
statusMessage = FindControl("StatusMessage") as StatusMessage; 85
86
username = FindControl("Username") as TextBox; 87
password = FindControl("Password") as TextBox; 88
89
verifyCode = FindControl("VerifyCode") as TextBox; 90
verifyCodeImage = FindControl("VerifyCodeImage") as Image; 91
92
loginButton = FindControl("LoginButton") as Button; 93
loginButton.Click += new EventHandler(LoginButton_Click); 94
95
TurnFrontButton = FindControl("TurnFrontButton") as HyperLink; 96
TurnFrontButton.NavigateUrl= "~/Default.aspx"; 97
} 98
99
protected override void OnLoad(EventArgs e) 100
{ 101
EnsureChildControls(); 102
base.OnLoad(e); 103
104
verifyCodeImage.ImageUrl = GlobalUrls.Instance().VerifyCodeImage(4, 15, true, true); 105
verifyCodeImage.ToolTip = "看不清?点击换一个验证码"; 106
verifyCodeImage.Attributes["onclick"] = "this.src=this.src+'&?'"; 107
} 108
109
protected void LoginButton_Click(Object sender, EventArgs e) 110
{ 111
string verifyCodeFromCookie = Globals.GetVerifyCodeFromCookie(Context); 112
if (!verifyCode.Text.Trim().Equals(verifyCodeFromCookie, StringComparison.CurrentCultureIgnoreCase)) 113
{ 114
CustomValidator cv = FindControl("CustomValidatorVerifyCode") as CustomValidator; 115
if (cv != null) 116
{ 117
cv.IsValid = false; 118
} 119
120
return; 121
} 122
123
if (!Page.IsValid) 124
return; 125
126
User userToLogin = new User(); 127
string redirectUrl = null; 128
129
if (!Page.IsValid) 130
return; 131
132
userToLogin.UserName = username.Text.Trim(); 133
userToLogin.Password = password.Text.Trim(); 134
135
LoginUserStatus loginStatus = Users.ValidUser(userToLogin.UserName, userToLogin.Password); 136
137
//如果作为用户名登录失败,则作为Email重试 138
if (loginStatus == LoginUserStatus.InvalidCredentials) 139
{ 140
User userByEmail = Users.FindUserByEmail(username.Text.Trim()); 141
if (userByEmail != null && !userByEmail.IsAnonymous) 142
{ 143
userToLogin.UserName = userByEmail.UserName; 144
loginStatus = Users.ValidUser(userToLogin.UserName, userToLogin.Password); 145
} 146
} 147
148
bool enableBannedUsersToLogin = SiteSettingsManager.GetSiteSettings().EnableBannedUsersToLogin; 149
150
// Change to let banned users in 151
// 152
if (loginStatus == LoginUserStatus.Success || (enableBannedUsersToLogin && loginStatus == LoginUserStatus.AccountBanned)) 153
{ 154
//写入Cookie,供后台其他页面调用 155
HttpCookie adminCookie = new HttpCookie("SpaceBuilderAdminCookie"); 156
adminCookie.Values["adminName"] = userToLogin.UserName; 157
//TunyNet.Security.HashEncrypt encry = new HashEncrypt(TunyNet.Security.HashEncrypt.HashEncryptType.MD5); 158
//string passEncryed = encry.CreateHash(userToLogin.Password); 159
//adminCookie.Values["adminPassword"] = passEncryed; 160
SBContext.Current.Context.Response.Cookies.Add(adminCookie); 161
162
163
FormsAuthentication.SetAuthCookie(userToLogin.UserName, false); 164
if (string.IsNullOrEmpty(SBContext.Current.ReturnUrl)) 165
{ 166
SBContext.Current.Context.Response.Redirect(ManagerUrls.Instance().ManageHome()); 167
} 168
else 169
{ 170
SBContext.Current.Context.Response.Redirect(SBContext.Current.ReturnUrl); 171
} 172
} 173
else if (loginStatus == LoginUserStatus.InvalidCredentials) 174
{ 175
// Invalid Credentials 176
//Context.Response.Redirect(GlobalUrls.Instance().Message(SBExceptionType.UserInvalidCredentials), true); 177
178
statusMessage.Visible = true; 179
statusMessage.MessageType = StatusMessageType.Error; 180
statusMessage.Text = "用户名或密码出错"; 181
} 182
else if (loginStatus == LoginUserStatus.AccountPending) 183
{ 184
// Account not approved yet 185
//Context.Response.Redirect(GlobalUrls.Instance().Message(SBExceptionType.UserAccountPending), true); 186
statusMessage.Visible = true; 187
statusMessage.MessageType = StatusMessageType.Error; 188
statusMessage.Text = "超级管理员正在审核您的帐号,审核通过后您将收到邮件通知"; 189
} 190
else if (loginStatus == LoginUserStatus.AccountDisapproved) 191
{ 192
// Account disapproved 193
//Context.Response.Redirect(GlobalUrls.Instance().Message(SBExceptionType.UserAccountDisapproved), true); 194
statusMessage.Visible = true; 195
statusMessage.MessageType = StatusMessageType.Error; 196
statusMessage.Text = "您的账户未通过审核,不能进行登录"; 197
} 198
else if (loginStatus == LoginUserStatus.UnknownError) 199
{ 200
// Unknown error because of miss-syncronization of internal data 201
throw new SBException(SBExceptionType.UserUnknownLoginError); 202
} 203
// Reject banned users if they are not allowed to 204
// pass through login. 205
// 206
else if (!enableBannedUsersToLogin && loginStatus == LoginUserStatus.AccountBanned) 207
{ 208
// Account banned 209
//Context.Response.Redirect(GlobalUrls.Instance().Message(SBExceptionType.UserAccountBanned), true); 210
statusMessage.Visible = true; 211
statusMessage.MessageType = StatusMessageType.Error; 212
statusMessage.Text = "您的账户处于封禁状态,不能登录系统"; 213
} 214
} 215
} 216
} 217





}