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

推荐订阅源

美团技术团队
Blog — PlanetScale
Blog — PlanetScale
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
博客园_首页
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
F
Fortinet All Blogs
腾讯CDC
罗磊的独立博客
IT之家
IT之家
I
InfoQ
V
V2EX
博客园 - 叶小钗
A
About on SuperTechFans
Y
Y Combinator Blog
C
Check Point Blog
量子位
Martin Fowler
Martin Fowler
Vercel News
Vercel News

博客园 - 杨发达

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)  评论()    收藏  举报