国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用HttpContext的User屬性來實現用戶驗證

2019-11-18 12:02:28
字體:
來源:轉載
供稿:網友

  HttpContext類包含了個別HTTP請求的所有特定HTTP信息。這個示例主要是講如何使用HttpContext類中的User屬性來實現用戶驗證!
  
  用戶驗證是大部分asp.net WEB應用程序都要用到的,它在整個應用程序中占有很重要的地位,在.NET中,包含了很多種用戶驗證方式,如眾所周知的PassPort認證,Windows認證,Form認證等等,可是這些都很難滿足我們在實際應用中的需求,以致于很多朋友都是自己另外寫代碼來實現自己需要的功能,這讓我們在安全性以及系統效率上要考慮很多。
  
  實際上,ASP.NET中內置的用戶驗證機制功能非常強大,同時也具有非常好的的可擴展性,它能夠在HttpContext對象中生成一個名為User的屬性,這個屬性能讓我們訪問各種信息,包括用戶是否已驗證,用戶的類型,用戶名等等,我們還可以對該屬性的功能進性擴展,以實現我們的要求。
  
  分配給HttpContext.User的對象必須實現iprincipal接口,而IPRincipal定義的屬性之一是Identity,它必須實現Iidentity接口。因為,我們只要寫了實現這兩個接口的類,就可以在這些類中添加任何我們所需要的功能。
  
  首先,我們創建兩個實現Iprincipal和Iidentity的類,分另為MyIprincipal和MyIdentity
  
  MyIprincipal.cs
  
  using System;
  
  using System.Collections;
  
  namespace HttpContextUserEG
  
  {
  
  /// <summary>
  
  /// MyPrincipal 的摘要說明。
  
  /// </summary>
  
  /// 實現IPrincipal接口
  
  public class MyPrincipal : System.Security.Principal.IPrincipal
  
  {
  
  private System.Security.Principal.IIdentity identity;
  
  private ArrayList roleList;
  
  public MyPrincipal(string userID,string passWord)
  
  {
  
  //
  
  // TODO: 在此處添加構造函數邏輯
  
  //
  
  identity = new MyIdentity(userID,password);
  
  if(identity.IsAuthenticated)
  
  {
  
  //假如通過驗證則獲取該用戶的Role,這里可以修改為從數據庫
  
  //讀取指定用戶的Role并將其添加到Role中,本例中直接為用戶添加一個Admin角色
  
  roleList = new ArrayList();
  
  roleList.Add("Admin");
  
  }
  
  else
  
  {
  
  // do nothing
  
  }
  
  }
  
  public ArrayList RoleList
  
  {
  
  get
  
  {
  
  return roleList;
  
  }
  
  }
  
  #region IPrincipal 成員
  
  public System.Security.Principal.IIdentity Identity
  
  {
  
  get
  
  {
  
  // TODO: 添加 MyPrincipal.Identity getter 實現
  
  return identity;
  
  }
  
  set
  
  {
  
  identity = value;
  
  }
  
  }
  
  public bool IsInRole(string role)
  
  {
  
  // TODO: 添加 MyPrincipal.IsInRole 實現
  
  return roleList.Contains(role);;
  
  }
  
  #endregion
  
  }
  
  }
  
  MyIdentity.cs
  
  using System;
  
  namespace HttpContextUserEG
  
  {
  
  /// <summary>
  
  /// MyIdentity 的摘要說明。
  
  /// </summary>
  
  /// 實現IIdentity接口
  
  public class MyIdentity : System.Security.Principal.IIdentity
  
  {
  
  private string userID;
  
  private string password;
  
  public MyIdentity(string currentUserID,string currentPassword)
  
  {
  
  //
  
  // TODO: 在此處添加構造函數邏輯
  
  //
  
  userID = currentUserID;
  
  password = currentPassword;
  
  }
  
  private bool CanPass()
  
  {
  
  //這里朋友們可以根據自己的需要改為從數據庫中驗證用戶名和密碼,
  
  //這里為了方便我直接指定的字符串
  
  if(userID == "yan0lovesha" && password == "iloveshasha")
  
  {
  
  return true;
  
  }
  
  else
  
  {
  
  return false;
  
  }
  
  }
  
  public string Password
  
  {
  
  get
  
  {
  
  return password;
  
  }
  
  set
  
  {
  
  password = value;
  
  }
  
  }
  
  #region IIdentity 成員
  
  public bool IsAuthenticated
  
  {
  
  get
  
  {
  
  // TODO: 添加 MyIdentity.IsAuthenticated getter 實現
  
  return CanPass();
  
  }
  
  }
  
  public string Name
  
  {
  
  get
  
  {
  
  // TODO: 添加 MyIdentity.Name getter 實現
  
  return userID;
  
  }
  
  }
  
  //這個屬性我們可以根據自己的需要來靈活使用,在本例中沒有用到它
  
  public string AuthenticationType
  
  {
  
  get
  
  {
  
  // TODO: 添加 MyIdentity.AuthenticationType getter 實現
  
  return null;
  
  }
  
  }
  
  #endregion
  
  }
  
  }
  
  在完成了這兩個類之后我們還要創建一個自己的Page類,來配合我們的驗證,這里我們將其命名為MyPage,繼續自Page類
  
  MyPage.cs
  
  using System;
  
  using System.Collections;
  
  namespace HttpContextUserEG
  
  {
  
  /// <summary>
  
  /// MyPage 的摘要說明。
  
  /// </summary>
  
  /// 繼續自Page類
  
  public class MyPage : System.Web.UI.Page
  
  {
  
  public MyPage()
  
  {
  
  //
  
  // TODO: 在此處添加構造函數邏輯
  
  //
  
  }
  
  protected override void OnInit(EventArgs e)
  
  {
  
  base.OnInit (e);
  
  this.Load +=new EventHandler(MyPage_Load);
  
  }
  
  //在頁面加載的時候從緩存中提取用戶信息
  
  private void MyPage_Load(object sender, System.EventArgs e)
  
  {
  
  if(Context.User.Identity.IsAuthenticated)
  
  {
  
  if(Context.Cache["UserMessage"] != null)
  
  {
  
  Hashtable userMessage = (Hashtable)Context.Cache["UserMessage"];
  
  MyPrincipal principal = new MyPrincipal(userMessage["UserID"].ToString(),userMessage["UserPassword"].ToString());
  
  Context.User = principal;
  
  }
  
  }
  
  }
  
  }
  
  }
  
  下面就是我們的界面WebForm.aspx和WebForm.aspx.cs
  
  WebForm.aspx
  
  <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="HttpContextUserEG.WebForm1" %>
  
  <!DOCTYPE Html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
  
  <HTML>
  
  <HEAD>
  
  <title>WebForm1</title>
  
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  
  <meta content="C#" name="CODE_LANGUAGE">
  
  <meta content="javascript" name="vs_defaultClientScript">
  
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  
  </HEAD>
  
  <body>
  
  <form id="Form1" method="post" runat="server">
  
  <P><FONT face="宋體">用戶名:
  
  <asp:TextBox id="tbxUserID" runat="server"></asp:TextBox><BR>
  
  密 碼:
  
  <asp:TextBox id="tbXPassword" runat="server" TextMode="Password"></asp:TextBox></FONT></P>
  
  <P><FONT face="宋體">
  
  <asp:Button id="BTnLogin" runat="server" Text="登錄"></asp:Button>
  
  <asp:Label id="lblLoginMessage" runat="server"></asp:Label></FONT></P>
  
  <

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南皮县| 海原县| 桂林市| 沾化县| 乐都县| 榆树市| 改则县| 潞西市| 南京市| 鹤庆县| 昌平区| 平原县| 泸西县| 临沧市| 乳源| 板桥市| 崇礼县| 集贤县| 宜良县| 泸水县| 运城市| 建瓯市| 南京市| 民乐县| 长沙县| 鄂温| 得荣县| 桦南县| 巴南区| 布尔津县| 夏河县| 武清区| 六盘水市| 浑源县| 电白县| 保康县| 进贤县| 盐津县| 甘孜县| 永仁县| 丘北县|