惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
美团技术团队
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
A
About on SuperTechFans
Recent Announcements
Recent Announcements
D
Docker
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
腾讯CDC
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志

博客园 - 杨发达

Tracking User Activity export to csv + excel 打开乱码的问题 iis asp.net window 认证中的问题。 - 杨发达 sqlserver+job+ 超时 - 杨发达 - 博客园 在XML文件中有些字符是被禁止使用的 在ASP.NET应用程序中使用身份模拟+2.0 Snap 链接预览的应用 sql2005 fulltext search : 根据menu的配置项,执行定义的event How to generate an RSS feed for your web site using ASP.NET - 杨发达 .net Application Localization的db 实现 如何制作Mht 文件 + dotnet Composite UI Application Block Study (前) 开源的SSO方案--SourceID.NET OLAP 资源 使用CAB 建立Like Dnn 的桌面应用系统框架- window form application framework olap 常用的前台 Control Javascript: 设置textbox 为readonly . install window service
在ASP.NET应用程序中使用身份模拟(Impersonation) - 杨发达...
杨发达 · 2008-11-24 · via 博客园 - 杨发达

<%@ Page Language="C#"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern int LogonUser(String lpszUserName,
                 String lpszDomain,
                 String lpszPassword,
                 int dwLogonType,
                 int dwLogonProvider,
                 ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
public extern static int DuplicateToken(IntPtr hToken,
                 int impersonationLevel, 
                 ref IntPtr hNewToken);
public void Page_Load(Object s, EventArgs e)
{
  if(impersonateValidUser("username", "domain", "password"))
  {
   //Insert your code that runs under the security context of a specific user here.
   undoImpersonation();
  }
  else
  {
   //Your impersonation failed. Therefore, include a fail-safe mechanism here.
  }
}
private bool impersonateValidUser(String userName, String domain, String password)
{
  WindowsIdentity tempWindowsIdentity;
  IntPtr token = IntPtr.Zero;
  IntPtr tokenDuplicate = IntPtr.Zero;
  if(LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
  LOGON32_PROVIDER_DEFAULT, ref token) != 0)
  {
   if(DuplicateToken(token, 2, ref tokenDuplicate) != 0)
   {
     tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
     impersonationContext = tempWindowsIdentity.Impersonate();
     if (impersonationContext != null)
      return true;
     else
      return false;
   }
   else
     return false;
  }
  else
   return false;
}
private void undoImpersonation()
{
   impersonationContext.Undo();
}
</script>

posted on 2008-11-24 18:00  杨发达  阅读(417)  评论()    收藏  举报