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

推荐订阅源

WordPress大学
WordPress大学
GbyAI
GbyAI
P
Proofpoint News Feed
B
Blog
MyScale Blog
MyScale Blog
V
V2EX
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
量子位
Jina AI
Jina AI
博客园 - 叶小钗
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
罗磊的独立博客
L
LangChain Blog
I
InfoQ
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
小众软件
小众软件
V
Visual Studio Blog
月光博客
月光博客
The Cloudflare Blog
雷峰网
雷峰网

博客园 - 杨发达

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