国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

通過ASP.net程序創建域帳戶故障

2024-07-10 12:56:41
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 我曾經成功地使用windows程序成功的創建了一批帶郵箱的域帳戶,但是,當我把這段代碼交給我的一個同事(她負責開發web應用)遷移到asp.net中后,只能創建域帳戶,不能創建郵箱。為什么呢?

    我們咨詢了微軟的工程師,他告訴我們,這是由于asp.net的權限不夠,我們應該在asp.net模擬用戶,這樣就可以成功創建。

    我將微軟的相關文章摘錄下來:



    模擬 iis 驗證的帳戶或用戶

    若要在收到 asp.net 應用程序中每個頁的每個請求時模擬 microsoft internet 信息服務 (iis) 身份驗證用戶,必須在此應用程序的 web.config 文件中包含 <identity> 標記,并將 impersonate 屬性設置為 true。例如:

    <identity impersonate="true" />


    為 asp.net 應用程序的所有請求模擬特定用戶

    若要為 asp.net 應用程序的所有頁面上的所有請求模擬特定用戶,可以在該應用程序的 web.config 文件的 <identity> 標記中指定 username 和 password 屬性。例如:

    <identity impersonate="true" username="accountname" password="password" />


    注意:在線程上模擬特定用戶的進程的標識必須具有“作為操作系統的一部分”權限。默認情況下,aspnet_wp.exe 進程在名為 aspnet 的計算機帳戶下運行。不過,此帳戶沒有模擬特定用戶所需的權限。如果您嘗試模擬特定用戶,則會出現一條錯誤信息。

    要解決此問題,請使用下列方法之一:

    &#8226;

    為 aspnet 帳戶(權限最低的帳戶)授予“作為操作系統的一部分”權限。

    注意:雖然此方法可以解決問題,但 microsoft 不建議使用此方法。

    &#8226;

    在 machine.config 文件的 <processmodel> 配置部分中,將運行 aspnet_wp.exe 進程所使用的帳戶更改為 system 帳戶。

    在代碼中模擬身份驗證用戶

    若要僅在運行代碼特定部分時模擬身份驗證用戶 (user.identity),您可以使用以下代碼。此方法要求身份驗證用戶標識的類型為 windowsidentity。

    visual basic .net

    dim impersonationcontext as system.security.principal.windowsimpersonationcontext
    dim currentwindowsidentity as system.security.principal.windowsidentity
    currentwindowsidentity = ctype(user.identity, system.security.principal.windowsidentity)
    impersonationcontext = currentwindowsidentity.impersonate()
    'insert your code that runs under the security context of the authenticating user here.
    impersonationcontext.undo()


    visual c# .net

    system.security.principal.windowsimpersonationcontext impersonationcontext;
    impersonationcontext =
    ((system.security.principal.windowsidentity)user.identity).impersonate();
    //insert your code that runs under the security context of the authenticating user here.
    impersonationcontext.undo();


    visual j# .net

    system.security.principal.windowsimpersonationcontext impersonationcontext;
    impersonationcontext =
    ((system.security.principal.windowsidentity)get_user().get_identity()).impersonate();
    //insert your code that runs under the security context of the authenticating user here.
    impersonationcontext.undo();


    在代碼中模擬特定用戶

    若要僅在運行代碼特定部分時模擬特定用戶,請使用以下代碼:

    visual basic .net

    <%@ page language="vb" %>
    <%@ import namespace = "system.web" %>
    <%@ import namespace = "system.web.security" %>
    <%@ import namespace = "system.security.principal" %>
    <%@ import namespace = "system.runtime.interopservices" %>
    <script runat=server>
    dim logon32_logon_interactive as integer = 2
    dim logon32_provider_default as integer = 0
    dim impersonationcontext as windowsimpersonationcontext
    declare function logonusera lib "advapi32.dll" (byval lpszusername as string, _
    byval lpszdomain as string, _
    byval lpszpassword as string, _
    byval dwlogontype as integer, _
    byval dwlogonprovider as integer, _
    byref phtoken as intptr) as integer
    declare auto function duplicatetoken lib "advapi32.dll" ( _
    byval existingtokenhandle as intptr, _
    byval impersonationlevel as integer, _
    byref duplicatetokenhandle as intptr) as integer
    declare auto function reverttoself lib "advapi32.dll" () as long
    declare auto function closehandle lib "kernel32.dll" (byval handle as intptr) as long
    public sub page_load(byval s as object, byval e as eventargs)
    if impersonatevaliduser("username", "domain", "password") then
    '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.
    end if
    end sub
    private function impersonatevaliduser(byval username as string, _
    byval domain as string, byval password as string) as boolean
    dim tempwindowsidentity as windowsidentity
    dim token as intptr = intptr.zero
    dim tokenduplicate as intptr = intptr.zero
    impersonatevaliduser = false
    if reverttoself() then
    if logonusera(username, domain, password, logon32_logon_interactive,
    logon32_provider_default, token) <> 0 then
    if duplicatetoken(token, 2, tokenduplicate) <> 0 then
    tempwindowsidentity = new windowsidentity(tokenduplicate)
    impersonationcontext = tempwindowsidentity.impersonate()
    if not impersonationcontext is nothing then
    impersonatevaliduser = true
    end if
    end if
    end if
    end if
    if not tokenduplicate.equals(intptr.zero) then
    closehandle(tokenduplicate)
    end if
    if not token.equals(intptr.zero) then
    closehandle(token)
    end if
    end function
    private sub undoimpersonation()
    impersonationcontext.undo()
    end sub
    </script>

    visual c# .net

    <%@ 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")]
    public static extern int logonusera(string lpszusername,
    string lpszdomain,
    string lpszpassword,
    int dwlogontype,
    int dwlogonprovider,
    ref intptr phtoken);
    [dllimport("advapi32.dll", charset=charset.auto, setlasterror=true)]
    public static extern int duplicatetoken(intptr htoken,
    int impersonationlevel,
    ref intptr hnewtoken);
    [dllimport("advapi32.dll", charset=charset.auto, setlasterror=true)]
    public static extern bool reverttoself();
    [dllimport("kernel32.dll", charset=charset.auto)]
    public static extern bool closehandle(intptr handle);
    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(reverttoself())
    {
    if(logonusera(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)
    {
    closehandle(token);
    closehandle(tokenduplicate);
    return true;
    }
    }
    }
    }
    if(token!= intptr.zero)
    closehandle(token);
    if(tokenduplicate!=intptr.zero)
    closehandle(tokenduplicate);
    return false;
    }
    private void undoimpersonation()
    {
    impersonationcontext.undo();
    }
    </script>

    visual j# .net

    <%@ page language="vj#" %>
    <%@ import namespace="system.web" %>
    <%@ import namespace="system.web.security" %>
    <%@ import namespace="system.security.principal" %>
    <%@ import namespace="system.runtime.interopservices" %>
    <script runat=server>
    public static int logon32_logon_interactive = 2;
    public static int logon32_provider_default = 0;
    windowsimpersonationcontext impersonationcontext;
    /** @attribute dllimport("advapi32.dll") */
    public static native int logonusera(string lpszusername,
    string lpszdomain,
    string lpszpassword,
    int dwlogontype,
    int dwlogonprovider,
    system.intptr[] phtoken);
    /** @attribute dllimport("advapi32.dll",
    charset=charset.auto, setlasterror=true) */
    public static native int duplicatetoken(system.intptr htoken,
    int impersonationlevel,
    system.intptr[] hnewtoken);
    /** @attribute dllimport("kernel32.dll",charset=charset.auto) */
    public static native boolean closehandle(system.intptr[] handle);
    /** @attribute dllimport("advapi32.dll",
    charset=charset.auto,setlasterror=true) */
    public static native boolean reverttoself();
    public void page_load(object s, system.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 boolean impersonatevaliduser(string username, string domain, string password)
    {
    windowsidentity tempwindowsidentity;
    system.intptr[] token = new system.intptr[1];
    system.intptr[] tokenduplicate = new system.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 = new windowsidentity(tokenduplicate[0]);
    impersonationcontext = tempwindowsidentity.impersonate();
    if (impersonationcontext != null)
    {
    closehandle(tokenduplicate);
    closehandle(token);
    return true;
    }
    }
    }
    }
    if(!token[0].equals(system.intptr.zero))
    closehandle(token);
    if(!tokenduplicate[0].equals(system.intptr.zero))
    closehandle(tokenduplicate);
    return false;
    }
    private void undoimpersonation()
    {
    impersonationcontext.undo();
    }
    </script>


    注意:在線程上模擬特定用戶的進程的標識必須具有“作為操作系統的一部分”權限。默認情況下,aspnet_wp.exe 進程在名為 aspnet 的計算機帳戶下運行。不過,此帳戶沒有模擬特定用戶所需的權限。如果您嘗試模擬特定用戶,則會出現一條錯誤信息。

    要解決此問題,請使用下列方法之一:

    &#8226;

    為 aspnet 帳戶授予“作為操作系統的一部分”權限。

    &#8226;

    在 machine.config 文件的 <processmodel> 配置部分中,將運行 aspnet_wp.exe 進程所使用的帳戶更改為 system 帳戶。

    返回頁首



    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 祁连县| 浮山县| 黔西| 舟山市| 湄潭县| 延安市| 诸暨市| 大厂| 阿鲁科尔沁旗| 大渡口区| 邢台市| 甘谷县| 永寿县| 化隆| 五家渠市| 桃园市| 镇巴县| 光山县| 阜南县| 综艺| 芦山县| 闵行区| 华坪县| 扬州市| 和田市| 文登市| 扎囊县| 木兰县| 来宾市| 秦皇岛市| 余江县| 平湖市| 四平市| 寻乌县| 拉孜县| 嘉禾县| 交城县| 武穴市| 河北区| 安龙县| 镇原县|