📄 login.cs
字号:
namespace ASPNET.StarterKit.Communities {
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.Communities;
using System.Web.Security;
//*********************************************************************
//
// Login Class
//
// Represents the user login page which enables
// users to login to the community.
//
//*********************************************************************
public class Login : SkinnedCommunityControl {
string _skinFileName = "Users_Login.ascx";
TextBox txtUsername;
TextBox txtPassword;
CheckBox chkPersist;
Button btnLogin;
Panel pnlInvalidUsername;
Panel pnlInvalidPassword;
HyperLink lnkPasswordReminder;
//*********************************************************************
//
// Login Constructor
//
// Calls the base SkinnedCommunityControl constructor
// and assigns the default page skin.
//
//*********************************************************************
public Login() : base() {
// Assign a default skin file name
if (SkinFileName == null)
SkinFileName = _skinFileName;
}
//*********************************************************************
//
// SkinType Property
//
// Specifies the skins directory where this page's skin file is located.
//
//*********************************************************************
override protected string SkinType {
get { return "ContentSkins"; }
}
//*********************************************************************
//
// InitializeSkin Method
//
// Retrieves all the controls from the Page Skin
//
//*********************************************************************
override protected void InitializeSkin(Control skin) {
// Find the Username TextBox
txtUsername = (TextBox)GetControl(skin, "txtUsername");
// Find the Password TextBox
txtPassword = (TextBox)GetControl(skin, "txtPassword");
txtPassword.TextChanged += new System.EventHandler(btnLogin_Click);
// Find the Persist Checkbox (Optional)
chkPersist = (CheckBox)GetOptionalControl(skin, "chkPersist");
// Find the Invalid Username Panel
pnlInvalidUsername = (Panel)GetControl(skin, "pnlInvalidUsername");
pnlInvalidUsername.Visible = false;
// Find the Invalid Password Panel
pnlInvalidPassword = (Panel)GetControl(skin, "pnlInvalidPassword");
pnlInvalidPassword.Visible = false;
// Find Login Button
btnLogin = (Button)GetControl(skin, "btnLogin");
btnLogin.Click += new System.EventHandler(btnLogin_Click);
// Find password reminder
lnkPasswordReminder = (HyperLink)GetControl(skin, "lnkPasswordReminder");
lnkPasswordReminder.NavigateUrl = "~/Users_PasswordReminder.aspx";
}
//*********************************************************************
//
// btnLogin_Click Method
//
// Checks username and password against database. If
// everything checks, logins the user.
//
//*********************************************************************
void btnLogin_Click(Object s, EventArgs e) {
bool blPersist = false;
// Determine whether password should be persisted
if (chkPersist != null)
blPersist = chkPersist.Checked;
// Either login or display error message
switch ( UserUtility.LoginUser(txtUsername.Text,txtPassword.Text) ) {
case 0: // Success!
FormsAuthentication.SetAuthCookie(txtUsername.Text, blPersist);
string redirectUrl = FormsAuthentication.GetRedirectUrl(txtUsername.Text, blPersist).ToLower();
if (redirectUrl.IndexOf("users_logout.aspx") == -1)
Context.Response.Redirect(redirectUrl);
else
Context.Response.Redirect(CommunityGlobals.ResolveBase("default.aspx"));
break;
case 1: // Invalid Password
pnlInvalidPassword.Visible = true;
pnlInvalidUsername.Visible = false;
break;
case 2: // Invalid Username
pnlInvalidUsername.Visible = true;
pnlInvalidPassword.Visible = false;
break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -