我需要獲取當前登錄的用戶名?當我從Windows身份驗證模式下工作的ASP.NET調用代碼時,我需要這樣才能正常工作。即我不想在這種情況下獲得ASPNET用戶,但是模擬的用戶。這與我以前的question有關。我所嘗試的一切都會返回ASPNET。如何獲取當前登錄的用戶,包括Delphi 2009中的域名?
回答
在您的其他問題,你寫你自己配置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 - 所有的最不強大的用戶)。
也許你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;
希望這有助於。
我也嘗試過。同樣的問題。 – Steve 2009-11-26 16:30:15
是的,因爲代碼似乎不起作用。 ;-)我試圖找出原因。 – 2009-11-26 17:00:01
這是我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);
我補充說這是作爲TOndrej代碼的替代品,因爲他的模擬只適用於當前線程。 – Remko 2009-11-26 20:45:54
- 1. pysvn:獲取當前登錄用戶名
- 2. 如何在Delphi Win64中獲取當前用戶的全名
- 3. Python:如何獲取登錄用戶的域\用戶名
- 4. 如何獲取當前登錄用戶的用戶對象
- 5. MGTwitterEngine獲取當前登錄的用戶名和名稱
- 6. PHP - 獲取當前腳本的名稱和目錄,不包括域名?
- 7. jquery SPGetCurrentUser()獲取sharepoint 2007中當前登錄的用戶名
- 8. 獲取當前登錄用戶asp.net
- 9. 如何在wordpress中獲取當前登錄用戶的角色?
- 10. 如何在Access VBA中獲取當前登錄的Windows用戶?
- 11. 如何獲取Pinax中當前登錄的用戶?
- 12. 如何獲取mysql中當前登錄的用戶?
- 13. 獲取當前登錄用戶PHP
- 14. QT獲取當前登錄用戶ID
- 15. ASP.NET MVC4 - Session_End - 如何在Global.asax的Session_End中獲取當前登錄的用戶名?
- 16. 獲取當前登錄用戶
- 17. 如何獲取當前登錄的活動目錄用戶名C#
- 18. 如何使用NAnt獲取當前登錄的Windows用戶
- 19. 如何獲取當前登錄的用戶的ID在pyrocms?
- 20. 如何使用Wordpress Rest Api獲取當前登錄的用戶?
- 21. 如何獲取當前登錄的access_token?
- 22. 在Laravel控制器中獲取當前登錄用戶名
- 23. 如何獲取當前用戶登錄grails的id?
- 24. 如何獲取當前登錄用戶的ID?
- 25. Grails:如何獲取有關當前登錄用戶的信息?
- 26. Spree - 如何獲取當前登錄的管理員用戶
- 27. 如何從當前登錄的用戶獲取user_id?
- 28. 如何在ViewComponent中獲取當前登錄用戶Asp.Net核心
- 29. fbjs:獲取當前的登錄用戶的用戶
- 30. 從活動目錄獲取當前登錄用戶的列表
的asp.net應用程序返回模擬的憑據。所以我知道這是設置正確。 – Steve 2009-11-26 15:10:38
我不確定我的Delphi應用程序一旦得到它就可以或者會用身份上下文來做什麼。記得Delphi是Win32 – Steve 2009-11-26 16:33:05
如何從ASP.NET調用Delphi應用程序?你能顯示一些代碼嗎? – mjn 2009-11-26 16:58:35