2009-11-26 76 views

回答

3

在您的其他問題,你寫你自己配置ASP.NET使用Windows身份驗證與模擬:

<system.web> 
    ... 
    <authentication mode="Windows"/> 
    <identity impersonate="true"/> 
    ... 
</system.web> 

是否ASP.NET應用程序顯示正確的憑據(用戶和域)?

你調用使用正確的身份背景德爾福的功能,如

WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity; 
try 
{ 
    ctx = winId.Impersonate(); 
    // call Delphi function, passing the identity context 
} 
catch 
{ 
} 
finally 
{ 
    if (ctx != null) 
     ctx.Undo(); 
} 

更新:

如果COM赤貧從後面的代碼的Web表單頁面調用,您可以嘗試將Web窗體頁面的ASPCOMPAT屬性設置爲true。

參見:

的 「身份」 標籤可以確保 線程執行的請求(MTA 線程)將模擬其安全 上下文用戶在 標記中指定,但我們的STA COM對象 最終在默認的 STA線程上創建這不是模仿, 導致它得到過程的安全上下文 (這是IUSR_XXX - 所有的最不強大的用戶)。

+0

的asp.net應用程序返回模擬的憑據。所以我知道這是設置正確。 – Steve 2009-11-26 15:10:38

+0

我不確定我的Delphi應用程序一旦得到它就可以或者會用身份上下文來做什麼。記得Delphi是Win32 – Steve 2009-11-26 16:33:05

+0

如何從ASP.NET調用Delphi應用程序?你能顯示一些代碼嗎? – mjn 2009-11-26 16:58:35

1

也許你IADsWinNTSystemInfo方法(從鏈接上一個問題)返回當前進程的帳戶信息,但ASP.NET正在模擬一個線程級別?

試試這個:

type 
    PTokenUser = ^TTokenUser; 
    TTokenUser = packed record 
    User: SID_AND_ATTRIBUTES; 
    end; 

function GetCurrentUserName(out DomainName, UserName: string): Boolean; 
var 
    Token: THandle; 
    InfoSize, UserNameSize, DomainNameSize: Cardinal; 
    User: PTokenUser; 
    Use: SID_NAME_USE; 
    _DomainName, _UserName: array[0..255] of Char; 
begin 
    Result := False; 
    DomainName := ''; 
    UserName := ''; 

    Token := 0; 
    if not OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, Token) then 
    begin 
    if GetLastError = ERROR_NO_TOKEN then // current thread is not impersonating, try process token 
    begin 
     if not OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, Token) then 
     Exit; 
    end 
    else 
     Exit; 
    end; 
    try 
    GetTokenInformation(Token, TokenUser, nil, 0, InfoSize); 
    User := AllocMem(InfoSize * 2); 
    try 
     if GetTokenInformation(Token, TokenUser, User, InfoSize * 2, InfoSize) then 
     begin 
     DomainNameSize := SizeOf(_DomainName); 
     UserNameSize := SizeOf(_UserName); 

     Result := LookupAccountSid(nil, User^.User.Sid, _UserName, UserNameSize, _DomainName, DomainNameSize, Use); 

     if Result then 
     begin 
      SetString(DomainName, _DomainName, StrLen(_DomainName)); 
      SetString(UserName, _UserName, StrLen(_UserName)); 
     end; 
     end; 
    finally 
     FreeMem(User); 
    end; 
    finally 
    CloseHandle(Token); 
    end; 
end; 

用法示例:

var 
    DomainName, UserName: string; 
begin 
    if not GetCurrentUserName(DomainName, UserName) then 
    RaiseLastOSError; 
    Writeln(Format('%s\%s', [DomainName, UserName])); 
end; 

希望這有助於。

+0

我也嘗試過。同樣的問題。 – Steve 2009-11-26 16:30:15

+0

是的,因爲代碼似乎不起作用。 ;-)我試圖找出原因。 – 2009-11-26 17:00:01

1

這是我LoadProfile工具的代碼的一部分,它運作良好,在2010年德爾福:

const 
    UNLEN = 256; // Maximum user name length 

var 
    TokenHandle: THandle; // Handle to the Processes' Acces Token 
    cbTokenInfo: DWORD; // Size of TokenInfo in Bytes 
    pTokenUser: PTOKEN_USER; // Pointer to a TOKEN_USER record 

    cchName: DWORD; // Count of characters (length) of the Username array 
    cchDomain: DWORD; // Count of characters (length) of the Domainname array 
    peUse: DWORD; // Account type for LookupAccountSid 

    UserName: array[0..UNLEN] of Char; // Holds the Username 
    DomainName: array[0..UNLEN] of Char; // Holds the Domainname 
    ComputerName: array[0..UNLEN] of Char; // Hold the Computername 


    // Open the Current Process' Token 
    OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY or 
     TOKEN_IMPERSONATE or TOKEN_DUPLICATE, TokenHandle); 

    // Check if we have a valid handle 
    if TokenHandle = 0 then 
     Exit; 

    { We will use GetTokenInformation to get the user's SID, the first call 
     to GetTokenInformation is used to determine how much memory we need to 
     allocate } 
    GetTokenInformation(TokenHandle, TokenUser, nil, 0, cbTokenInfo); 
    // as documented the call should fail with ERROR_INSUFFICIENT_BUFFER 
    if (GetLastError() <> ERROR_INSUFFICIENT_BUFFER) then 
     Exit; 

    // Allocate Memory 
    pTokenUser := HeapAlloc(GetProcessHeap(), 0, cbTokenInfo); 
    if (pTokenUser = nil) then 
     Exit; 

    // Retrieve the user information from the token. 
    if (not GetTokenInformation(TokenHandle, TokenUser, pTokenUser, 
     cbTokenInfo, cbTokenInfo)) then 
     Exit; 

    cchName := Length(UserName); 
    cchDomain := Length(DomainName); 
    peUse:= SidTypeUser; 


    // Use the SID to find User and Domain Name 
    Write('LookupAccountSid... '); 
    if not LookupAccountSid(nil, pTokenUser^.User.Sid, UserName, cchName, 
     DomainName, cchDomain, peUse) then 
     Exit; 

    // Cleanup 
    if (pTokenUser <> nil) then 
     HeapFree(GetProcessHeap(), 0, pTokenUser); 

    WriteLn('CloseHandle... OK'); 
    CloseHandle(TokenHandle); 
+0

我補充說這是作爲TOndrej代碼的替代品,因爲他的模擬只適用於當前線程。 – Remko 2009-11-26 20:45:54

相關問題