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

简介:

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

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

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

模拟IIS验证的帐户或用户

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


<identityimpersonate="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帐户。

目录
相关文章
|
3月前
|
开发框架 前端开发 小程序
分享46个ASP.NET博客程序源码,总有一款适合您
分享46个ASP.NET博客程序源码,总有一款适合您
24 0
|
4月前
|
存储 开发框架 .NET
Asp.Net第一章入门之后台处理程序
Asp.Net第一章入门之后台处理程序
30 0
|
6月前
|
开发框架 .NET Windows
windows 本地443端口被占用,ASP.NET Core程序拒绝访问
windows 本地443端口被占用,ASP.NET Core程序拒绝访问背景:本地的ASP.NET Core程序在配置了HTTPS并且监听443端口,通过https访问被拒绝,提示没有权限,输入swagger路径访问swagger也提示未找到相关页面。解决:一般情况下,访问我们的ASP.NET Core 程序的swagger页面并不需要什么权限,而且本地调试运行swagger文档也没有什...
58 1
|
开发框架 前端开发 .NET
ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中
ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中
119 0
ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中
|
Web App开发 开发框架 Kubernetes
临近年关,修复ASP.NET Core因浏览器内核版本引发的单点登录故障
经过测试, 出现单点登陆故障的是搜狗、360等双核浏览器(默认使用Chrome内核), 较新式的Edge、Chrome、Firefox均未出现此障碍。
临近年关,修复ASP.NET Core因浏览器内核版本引发的单点登录故障
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
129 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
|
开发框架 前端开发 .NET
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
107 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(2)
|
开发框架 .NET 开发工具
|
.NET API 开发框架
ASP.NET Core中实现单体程序的事件发布/订阅
ASP.NET Core中实现单体程序的事件发布/订阅 背景# 事件发布/订阅是一种非常强大的模式,它可以帮助业务组件间实现完全解耦,不同的业务组件只依赖事件,只关注哪些事件是需要自己处理的,而不用关注谁来处理自己发布事件,事件追溯(Event Sourcing)也是基于事件发布/订阅的。
1566 0