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

ASP.NET
Asp.net利用JQuery弹出层加载数据代码
asp.net dataview做无限极分类的又一用法
asp.net ckeditor编辑器的使用方法
告别ADO.NET实现应用系统无缝切换的烦恼(总结篇)
asp.net 实现动态显示当前时间(不用javascript不考虑开销)
.net动态显示当前时间(客户端javascript)
asp.net 结合YUI 3.0小示例
asp.net 取消缓存相关问题说明
asp.net 计划任务管理程序实现,多线程任务加载
ASP.NET 跨页面传值方法
asp.net中url地址传送中文参数时的两种解决方案
Asp.net 菜单控件简洁版
asp.net jQuery Ajax用户登录功能的实现
asp.net SharpZipLib的压缩与解压问题
asp.net url重写后页面回传问题
asp.net与Discuz!NT整合集成实例教程
Discuz!NT 3与asp.net 整合的实例教程
测试控制台使用方法
.net 动态标题实现方法
asp.net *.ashx类型的文件使用说明

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 61 ::
收藏到网摘: 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帐户。