📄 manageusers.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
namespace SkyShark.NA
{
/// <summary>
/// Summary description for _default.
/// </summary>
public class WebManageUsers : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.HyperLink HyperLink4;
protected System.Web.UI.WebControls.HyperLink HyperLink5;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
protected System.Web.UI.WebControls.Label lblMessage;
protected SkyShark.NA.DataSet1 dataSet11;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox txtAddUserName;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox txtAddPassword;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.TextBox txtAddConfPassword;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.TextBox txtDelUserName;
protected System.Web.UI.WebControls.Button btnDelDelete;
protected System.Web.UI.WebControls.Label Label7;
protected System.Web.UI.WebControls.Button btnAddSubmit;
protected System.Web.UI.WebControls.Label txtUser;
protected System.Web.UI.WebControls.HyperLink HyperLink3;
protected System.Web.UI.WebControls.ListBox lstAddRole;
private void Page_Load(object sender, System.EventArgs e)
{
if (Session["usrRole"]==null)
{
Response.Redirect("..\\default.aspx");
}
if (!(Session["usrRole"].ToString()=="Admin"))
{
Response.Redirect("..\\default.aspx");
}
else
{
txtUser.Text="User: "+ Session["usrName"].ToString();
}
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet11 = new SkyShark.NA.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.btnDelDelete.Click += new System.EventHandler(this.btnDelDelete_Click);
this.btnAddSubmit.Click += new System.EventHandler(this.btnAddSubmit_Click);
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT Username FROM dtUsers";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=NPANDEY-D185;initial catalog=Skyshark;integrated security=SSPI;persis" +
"t security info=True;workstation id=NPANDEY-D185;packet size=4096";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO dtUsers (Username, Password, Role) VALUES (@Username, @Password, @Rol" +
"e); SELECT Username, Password, Role FROM dtUsers WHERE (Username = @Username)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Username", System.Data.SqlDbType.VarChar, 15, "Username"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 15, "Password"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Role", System.Data.SqlDbType.VarChar, 10, "Role"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE dtUsers SET Username = @Username, Password = @Password, Role = @Role, PasswordChanged = @PasswordChanged WHERE (Username = @Original_Username) AND (Password = @Original_Password) AND (PasswordChanged = @Original_PasswordChanged OR @Original_PasswordChanged IS NULL AND PasswordChanged IS NULL) AND (Role = @Original_Role); SELECT Username, Password, Role, PasswordChanged FROM dtUsers WHERE (Username = @Username)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Username", System.Data.SqlDbType.VarChar, 15, "Username"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 15, "Password"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Role", System.Data.SqlDbType.VarChar, 10, "Role"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PasswordChanged", System.Data.SqlDbType.Bit, 1, "PasswordChanged"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Username", System.Data.SqlDbType.VarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Username", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Password", System.Data.SqlDbType.VarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Password", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PasswordChanged", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PasswordChanged", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Role", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Role", System.Data.DataRowVersion.Original, null));
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "UPDATE dtUsers SET Role = \'Disabled\' WHERE (Username = @Original_Username)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Username", System.Data.SqlDbType.VarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Username", System.Data.DataRowVersion.Original, null));
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "dtUsers", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("Username", "Username"),
new System.Data.Common.DataColumnMapping("Password", "Password"),
new System.Data.Common.DataColumnMapping("Role", "Role"),
new System.Data.Common.DataColumnMapping("PasswordChanged", "PasswordChanged")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("en-US");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
}
#endregion
private void btnAddSubmit_Click(object sender, System.EventArgs e)
{
if (txtAddUserName.Text==null || txtAddUserName.Text=="" || txtAddPassword.Text==null || txtAddPassword.Text=="" || txtAddConfPassword.Text==null || txtAddConfPassword.Text=="")
{
lblMessage.Text="One or more required values are missing. Try again.";
}
if (Page.IsValid)
{
string username, password, role;
int selection;
role=lstAddRole.SelectedItem.Text;
username=txtAddUserName.Text.Trim();
password=txtAddPassword.Text.Trim();
selection=lstAddRole.SelectedIndex;
sqlConnection1.Open();
sqlDataAdapter1.Fill(dataSet11, "UserList");
sqlConnection1.Close();
foreach (DataRow myRow in dataSet11.Tables["UserList"].Rows)
{
if (myRow[0].ToString().Trim().ToLower()==username.ToLower())
{
lblMessage.Text="The user name already exists. Please try another user name";
return;
}
}
sqlDataAdapter1.InsertCommand.Parameters[0].Value=username;
sqlDataAdapter1.InsertCommand.Parameters[1].Value=password;
sqlDataAdapter1.InsertCommand.Parameters[2].Value=role;
sqlConnection1.Open();
sqlDataAdapter1.InsertCommand.ExecuteNonQuery();
sqlConnection1.Close();
MailAttachment attachment= new MailAttachment("c:\\Inetpub\\wwwroot\\SkyShark\\NA\\PrivacyPolicy.doc");
MailMessage email= new MailMessage();
email.Attachments.Add(attachment);
email.To=username + "@niit.com";
email.From="nitinp@niit.com";
email.Subject="Message from SkyShark Airlines";
email.Body="Dear " + username + ",\n\nYour account has been added " +
"to the SkyShark Airlines application. You can log on to the " +
"application at http://npandey-d185/skyshark. \n\nYour logon name" +
" is " + username + " and the password is password. Please change" +
" your password when you log on. \n\n By logging on to the application," +
" you agree to abide by the terms and conditions attached in the mail" +
"\n\n Happy Browsing.\n\n Network Administrator (SkyShark)";
SmtpMail.Send(email);
lblMessage.Text="User added successfully";
txtAddUserName.Text="";
dataSet11.Clear();
}
}
private void btnDelDelete_Click(object sender, System.EventArgs e)
{
string username=txtDelUserName.Text.Trim();
bool userexists=false;
if (username==null || username=="")
{
lblMessage.Text="Please specify a valid user name";
}
else
{
sqlConnection1.Open();
sqlDataAdapter1.Fill(dataSet11, "UserList");
sqlConnection1.Close();
foreach (DataRow myRow in dataSet11.Tables["UserList"].Rows)
{
if (myRow[0].ToString().Trim().ToLower()==username.ToLower())
{
userexists=true;
}
}
if (userexists==false)
{
lblMessage.Text="The user does not exist";
return;
}
sqlDataAdapter1.DeleteCommand.Parameters[0].Value=username;
sqlConnection1.Open();
sqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
sqlConnection1.Close();
lblMessage.Text="User disabled successfully";
txtDelUserName.Text="";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -