当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  在ASP.NET中创建安全的web站点(配置) 3

 在ASP.NET中创建安全的web站点(配置) 3

点击次数:30 次 发布日期:2008-11-26 11:36:30 作者:源代码网
源代码网推荐      读者看完上面的代码之后一定想问CCommonDB是哪里来的东东,这是我编写的一个类,用来处理用户登录信息的,如果成功则把相关信息写入session、Cookie和SQL数据库,同时跳到default.aspx页面。具体如下:
源代码网推荐  
源代码网推荐  CCommonDB.cs
源代码网推荐  
源代码网推荐  namespace secure.Components
源代码网推荐  {
源代码网推荐  public class CCommonDB : CSql
源代码网推荐  {
源代码网推荐  public CCommonDB() : base() { }
源代码网推荐  
源代码网推荐  public string AuthenticateUser(
源代码网推荐  System.Web.SessionState.HttpSessionState objSession, // Session Variable
源代码网推荐  System.Web.HttpResponse objResponse, // Response Variable
源代码网推荐  string email, // Login
源代码网推荐  string password, // Password
源代码网推荐  bool bPersist // Persist login
源代码网推荐  )
源代码网推荐  {
源代码网推荐  int nLoginID = 0;
源代码网推荐  int nLoginType = 0;
源代码网推荐  
源代码网推荐  // Log the user in
源代码网推荐  Login(email, password, ref nLoginID, ref nLoginType);
源代码网推荐  
源代码网推荐  if(nLoginID != 0) // Success
源代码网推荐  {
源代码网推荐  // Log the user in
源代码网推荐  System.Web.Security.FormsAuthentication.SetAuthCookie(nLoginID.ToString(), bPersist);
源代码网推荐  
源代码网推荐  // Set the session varaibles
源代码网推荐  objSession["loginID"] = nLoginID.ToString();
源代码网推荐  objSession["loginType"] = nLoginType.ToString();
源代码网推荐  
源代码网推荐  // Set cookie information incase they made it persistant
源代码网推荐  System.Web.HttpCookie wrapperCookie = new System.Web.HttpCookie("wrapper");
源代码网推荐  wrapperCookie.Value = objSession["wrapper"].ToString();
源代码网推荐  wrapperCookie.Expires = DateTime.Now.AddDays(30);
源代码网推荐  
源代码网推荐  System.Web.HttpCookie lgnTypeCookie = new System.Web.HttpCookie("loginType");
源代码网推荐  lgnTypeCookie.Value = objSession["loginType"].ToString();
源代码网推荐  lgnTypeCookie.Expires = DateTime.Now.AddDays(30);
源代码网推荐  
源代码网推荐  // Add the cookie to the response
源代码网推荐  objResponse.Cookies.Add(wrapperCookie);
源代码网推荐  objResponse.Cookies.Add(lgnTypeCookie);
源代码网推荐  
源代码网推荐  return "/candidate/default.aspx";
源代码网推荐  }
源代码网推荐  case 1: // Admin Login
源代码网推荐  {
源代码网推荐  return "/admin/default.aspx";
源代码网推荐  }
源代码网推荐  case 2: // Reporting Login
源代码网推荐  {
源代码网推荐  return "/reports/default.aspx";
源代码网推荐  }
源代码网推荐  default:
源代码网推荐  {
源代码网推荐  return string.Empty;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  return string.Empty;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  
源代码网推荐  /// <summary>
源代码网推荐  /// Verifies the login and password that were given
源代码网推荐  /// </summary>
源代码网推荐  /// <param name="email">the login</param>
源代码网推荐  /// <param name="password">the password</param>
源代码网推荐  /// <param name="nLoginID">returns the login id</param>
源代码网推荐  /// <param name="nLoginType">returns the login type</param>
源代码网推荐  public void Login(string email, string password, ref int nLoginID, ref int nLoginType)
源代码网推荐  {
源代码网推荐  ResetSql();
源代码网推荐  
源代码网推荐  DataSet ds = new DataSet();
源代码网推荐  
源代码网推荐  // Set our parameters
源代码网推荐  SqlParameter paramLogin = new SqlParameter("@username", SqlDbType.VarChar, 100);
源代码网推荐  paramLogin.Value = email;
源代码网推荐  
源代码网推荐  SqlParameter paramPassword = new SqlParameter("@password", SqlDbType.VarChar, 20);
源代码网推荐  paramPassword.Value = password;
源代码网推荐  
源代码网推荐  
源代码网推荐  Command.CommandType = CommandType.StoredProcedure;
源代码网推荐  Command.CommandText = "glbl_Login";
源代码网推荐  Command.Parameters.Add(paramLogin);
源代码网推荐  Command.Parameters.Add(paramPassword);
源代码网推荐  
源代码网推荐  Adapter.TableMappings.Add("Table", "Login");
源代码网推荐  Adapter.SelectCommand = Command;
源代码网推荐  Adapter.Fill(ds);
源代码网推荐  
源代码网推荐  if(ds.Tables.Count != 0)
源代码网推荐  {
源代码网推荐  DataRow row = ds.Tables[0].Rows[0];
源代码网推荐  
源代码网推荐  // Get the login id and the login type
源代码网推荐  nLoginID = Convert.ToInt32(row["Login_ID"].ToString());
源代码网推荐  nLoginType = Convert.ToInt32(row["Login_Type"].ToString());
源代码网推荐  }
源代码网推荐  else
源代码网推荐  {
源代码网推荐  nLoginID = 0;
源代码网推荐  nLoginType = 0;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  
源代码网推荐  abstract public class CSql
源代码网推荐  {
源代码网推荐  private SqlConnection sqlConnection; // Connection string
源代码网推荐  private SqlCommand sqlCommand; // Command
源代码网推荐  private SqlDataAdapter sqlDataAdapter; // Data Adapter
源代码网推荐  private DataSet sqlDataSet; // Data Set
源代码网推荐  
源代码网推荐  public CSql()
源代码网推荐  {
源代码网推荐  sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
源代码网推荐  sqlCommand = new SqlCommand();
源代码网推荐  sqlDataAdapter = new SqlDataAdapter();
源代码网推荐  sqlDataSet = new DataSet();
源代码网推荐  
源代码网推荐  sqlCommand.Connection = sqlConnection;
源代码网推荐  }
源代码网推荐  
源代码网推荐  /// <summary>
源代码网推荐  /// Access to our sql command
源代码网推荐  /// </summary>
源代码网推荐  protected SqlCommand Command
源代码网推荐  {
源代码网推荐  get { return sqlCommand; }
源代码网推荐  }
源代码网推荐  
源代码网推荐  /// <summary>
源代码网推荐  /// Access to our data adapter
源代码网推荐  /// </summary>
源代码网推荐  protected SqlDataAdapter Adapter
源代码网推荐  {
源代码网推荐  get { return sqlDataAdapter; }
源代码网推荐  }
源代码网推荐  
源代码网推荐  /// <summary>
源代码网推荐  /// Makes sure that everything is clear and ready for a new query
源代码网推荐  /// </summary>
源代码网推荐  protected void ResetSql()
源代码网推荐  {
源代码网推荐  if(sqlCommand != null)
源代码网推荐  {
源代码网推荐  sqlCommand = new SqlCommand();
源代码网推荐  sqlCommand.Connection = sqlConnection;
源代码网推荐  }
源代码网推荐  if(sqlDataAdapter != null)
源代码网推荐  sqlDataAdapter = new SqlDataAdapter();
源代码网推荐  
源代码网推荐  if(sqlDataSet != null)
源代码网推荐  sqlDataSet = new DataSet();
源代码网推荐  }
源代码网推荐  
源代码网推荐  /// <summary>
源代码网推荐  /// Runs our command and returns the dataset
源代码网推荐  /// </summary>
源代码网推荐  /// <returns>the data set</returns>
源代码网推荐  protected DataSet RunQuery()
源代码网推荐  {
源代码网推荐  sqlDataAdapter.SelectCommand = Command;
源代码网推荐  
源代码网推荐  sqlConnection.Open();
源代码网推荐  sqlConnection.Close();
源代码网推荐  
源代码网推荐  sqlDataAdapter.Fill(sqlDataSet);
源代码网推荐  
源代码网推荐  return sqlDataSet;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  
源代码网推荐  http://blog.csdn.net/tielu0144/archive/2007/02/05/1502894.aspx
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华