当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 通过ASP.net程序创建域帐户故障

ASP.NET
asp.net Server.MapPath方法注意事项
asp.net下常用的加密算法MD5、SHA-1应用代码
几个 ASP.NET 小技巧
ASP.NET内置对象之Application对象
ASP.NET使用正则表达式屏蔽垃圾信息
asp.net“服务器应用程序不可用” 解决方法
asp.net Linq把数据导出到Excel的代码
asp.net ext treepanel 动态加载XML的实现方法
silverlight2.0Beta版TextBox输入中文解决方法
asp.net转出json格式客户端显示时间
asp.net下Linq To Sql注意事项小结
ASP.NET动态加载用户控件的实现方法
asp.net web大文件上传带进度条实例代码
asp.net DZ论坛中根据IP地址取得所在地的代码
向asp.Net进发 数据库连接操作
.net 上传文件前所未有的简单
asp.net gridview代码绑定
asp.net中List的使用方法
Asp.net(C#)实现验证码功能代码
asp.NET开发中正则表达式中BUG分析

ASP.NET 中的 通过ASP.net程序创建域帐户故障


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 75 ::
收藏到网摘: n/a

我曾经成功地使用windows程序成功的创建了一批带邮箱的域帐户,但是,当我把这段代码交给我的一个同事(她负责开发Web应用)迁移到asp.net中后,只能创建域帐户,不能创建邮箱。为什么呢?

我们咨询了微软的工程师,他告诉我们,这是由于asp.net的权限不够,我们应该在asp.net模拟用户,这样就可以成功创建。

我将微软的相关文章摘录下来:

模拟IIS验证的帐户或用户

若要在收到ASP.NET应用程序中每个页的每个请求时模拟MicrosoftInternet信息服务(IIS)身份验证用户,必须在此应用程序的Web.config文件中包含<identity>标记,并将impersonate属性设置为true。例如:

为ASP.NET应用程序的所有请求模拟特定用户

若要为ASP.NET应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的Web.config文件的<identity>标记中指定userName和password属性。例如:


<identityimpersonate="true"userName="accountname"password="password"/>

注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe进程在名为ASPNET的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。

要解决此问题,请使用下列方法之一:

•为ASPNET帐户(权限最低的帐户)授予“作为操作系统的一部分”权限。

注意:虽然此方法可以解决问题,但Microsoft不建议使用此方法。

•在Machine.config文件的<processModel>配置部分中,将运行Aspnet_wp.exe进程所使用的帐户更改为System帐户。

在代码中模拟身份验证用户

若要仅在运行代码特定部分时模拟身份验证用户(User.Identity),您可以使用以下代码。此方法要求身份验证用户标识的类型为WindowsIdentity。

VisualBasic.NET


DimimpersonationContextAsSystem.Security.Principal.WindowsImpersonationContext
DimcurrentWindowsIdentityAsSystem.Security.Principal.WindowsIdentity
currentWindowsIdentity=CType(User.Identity,System.Security.Principal.WindowsIdentity)
impersonationContext=currentWindowsIdentity.Impersonate()
'Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere.
impersonationContext.Undo()


VisualC#.NET


System.Security.Principal.WindowsImpersonationContextimpersonationContext;
impersonationContext=
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
//Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere.
impersonationContext.Undo();

VisualJ#.NET


System.Security.Principal.WindowsImpersonationContextimpersonationContext;
impersonationContext=
((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
//Insertyourcodethatrunsunderthesecuritycontextoftheauthenticatinguserhere.
impersonationContext.Undo();


在代码中模拟特定用户

若要仅在运行代码特定部分时模拟特定用户,请使用以下代码:

VisualBasic.NET


<%@PageLanguage="VB"%>
<%@ImportNamespace="System.Web"%>
<%@ImportNamespace="System.Web.Security"%>
<%@ImportNamespace="System.Security.Principal"%>
<%@ImportNamespace="System.Runtime.InteropServices"%>
<scriptrunat=server>
DimLOGON32_LOGON_INTERACTIVEAsInteger=2
DimLOGON32_PROVIDER_DEFAULTAsInteger=0
DimimpersonationContextAsWindowsImpersonationContext
DeclareFunctionLogonUserALib"advapi32.dll"(ByVallpszUsernameAsString,_
ByVallpszDomainAsString,_
ByVallpszPasswordAsString,_
ByValdwLogonTypeAsInteger,_
ByValdwLogonProviderAsInteger,_
ByRefphTokenAsIntPtr)AsInteger
DeclareAutoFunctionDuplicateTokenLib"advapi32.dll"(_
ByValExistingTokenHandleAsIntPtr,_
ByValImpersonationLevelAsInteger,_
ByRefDuplicateTokenHandleAsIntPtr)AsInteger
DeclareAutoFunctionRevertToSelfLib"advapi32.dll"()AsLong
DeclareAutoFunctionCloseHandleLib"kernel32.dll"(ByValhandleAsIntPtr)AsLong
PublicSubPage_Load(ByValsAsObject,ByValeAsEventArgs)
IfimpersonateValidUser("username","domain","password")Then
'Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere.
undoImpersonation()
Else
'Yourimpersonationfailed.Therefore,includeafail-safemechanismhere.
EndIf
EndSub
PrivateFunctionimpersonateValidUser(ByValuserNameAsString,_
ByValdomainAsString,ByValpasswordAsString)AsBoolean
DimtempWindowsIdentityAsWindowsIdentity
DimtokenAsIntPtr=IntPtr.Zero
DimtokenDuplicateAsIntPtr=IntPtr.Zero
impersonateValidUser=False
IfRevertToSelf()Then
IfLogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,token)<>0Then
IfDuplicateToken(token,2,tokenDuplicate)<>0Then
tempWindowsIdentity=NewWindowsIdentity(tokenDuplicate)
impersonationContext=tempWindowsIdentity.Impersonate()
IfNotimpersonationContextIsNothingThen
impersonateValidUser=True
EndIf
EndIf
EndIf
EndIf
IfNottokenDuplicate.Equals(IntPtr.Zero)Then
CloseHandle(tokenDuplicate)
EndIf
IfNottoken.Equals(IntPtr.Zero)Then
CloseHandle(token)
EndIf
EndFunction
PrivateSubundoImpersonation()
impersonationContext.Undo()
EndSub
</script>

VisualC#.NET


<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Web"%>
<%@ImportNamespace="System.Web.Security"%>
<%@ImportNamespace="System.Security.Principal"%>
<%@ImportNamespace="System.Runtime.InteropServices"%>
<scriptrunat=server>
publicconstintLOGON32_LOGON_INTERACTIVE=2;
publicconstintLOGON32_PROVIDER_DEFAULT=0;
WindowsImpersonationContextimpersonationContext;
[DllImport("advapi32.dll")]
publicstaticexternintLogonUserA(StringlpszUserName,
StringlpszDomain,
StringlpszPassword,
intdwLogonType,
intdwLogonProvider,
refIntPtrphToken);
[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]
publicstaticexternintDuplicateToken(IntPtrhToken,
intimpersonationLevel,
refIntPtrhNewToken);
[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]
publicstaticexternboolRevertToSelf();
[DllImport("kernel32.dll",CharSet=CharSet.Auto)]
publicstaticexternboolCloseHandle(IntPtrhandle);
publicvoidPage_Load(Objects,EventArgse)
{
if(impersonateValidUser("username","domain","password"))
{
//Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere.
undoImpersonation();
}
else
{
//Yourimpersonationfailed.Therefore,includeafail-safemechanismhere.
}
}
privateboolimpersonateValidUser(StringuserName,Stringdomain,Stringpassword)
{
WindowsIdentitytempWindowsIdentity;
IntPtrtoken=IntPtr.Zero;
IntPtrtokenDuplicate=IntPtr.Zero;
if(RevertToSelf())
{
if(LogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,reftoken)!=0)
{
if(DuplicateToken(token,2,reftokenDuplicate)!=0)
{
tempWindowsIdentity=newWindowsIdentity(tokenDuplicate);
impersonationContext=tempWindowsIdentity.Impersonate();
if(impersonationContext!=null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
returntrue;
}
}
}
}
if(token!=IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
returnfalse;
}
privatevoidundoImpersonation()
{
impersonationContext.Undo();
}
</script>

VisualJ#.NET


<%@Pagelanguage="VJ#"%>
<%@ImportNamespace="System.Web"%>
<%@ImportNamespace="System.Web.Security"%>
<%@ImportNamespace="System.Security.Principal"%>
<%@ImportNamespace="System.Runtime.InteropServices"%>
<scriptrunat=server>
publicstaticintLOGON32_LOGON_INTERACTIVE=2;
publicstaticintLOGON32_PROVIDER_DEFAULT=0;
WindowsImpersonationContextimpersonationContext;
/**@attributeDllImport("advapi32.dll")*/
publicstaticnativeintLogonUserA(StringlpszUserName,
StringlpszDomain,
StringlpszPassword,
intdwLogonType,
intdwLogonProvider,
System.IntPtr[]phToken);
/**@attributeDllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true)*/
publicstaticnativeintDuplicateToken(System.IntPtrhToken,
intimpersonationLevel,
System.IntPtr[]hNewToken);
/**@attributeDllImport("kernel32.dll",CharSet=CharSet.Auto)*/
publicstaticnativebooleanCloseHandle(System.IntPtr[]handle);
/**@attributeDllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true)*/
publicstaticnativebooleanRevertToSelf();
publicvoidPage_Load(Objects,System.EventArgse)
{
if(impersonateValidUser("username","domain","password"))
{
//Insertyourcodethatrunsunderthesecuritycontextofaspecificuserhere.
undoImpersonation();
}
else
{
//Yourimpersonationfailed.Therefore,includeafail-safemechanismhere.
}
}
privatebooleanimpersonateValidUser(StringuserName,Stringdomain,Stringpassword)
{
WindowsIdentitytempWindowsIdentity;
System.IntPtr[]token=newSystem.IntPtr[1];
System.IntPtr[]tokenDuplicate=newSystem.IntPtr[1];
if(RevertToSelf())
{
if(LogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,token)!=0)
{
if(DuplicateToken(token[0],2,tokenDuplicate)!=0)
{
tempWindowsIdentity=newWindowsIdentity(tokenDuplicate[0]);
impersonationContext=tempWindowsIdentity.Impersonate();
if(impersonationContext!=null)
{
CloseHandle(tokenDuplicate);
CloseHandle(token);
returntrue;
}
}
}
}
if(!token[0].Equals(System.IntPtr.Zero))
CloseHandle(token);
if(!tokenDuplicate[0].Equals(System.IntPtr.Zero))
CloseHandle(tokenDuplicate);
returnfalse;
}
privatevoidundoImpersonation()
{
impersonationContext.Undo();
}
</script>

注意:在线程上模拟特定用户的进程的标识必须具有“作为操作系统的一部分”权限。默认情况下,Aspnet_wp.exe进程在名为ASPNET的计算机帐户下运行。不过,此帐户没有模拟特定用户所需的权限。如果您尝试模拟特定用户,则会出现一条错误信息。

要解决此问题,请使用下列方法之一:

•为ASPNET帐户授予“作为操作系统的一部分”权限。

•在Machine.config文件的<processModel>配置部分中,将运行Aspnet_wp.exe进程所使用的帐户更改为System帐户。