2013-03-19 38 views
2

我的要求是驗證用戶輸入的密碼是他登錄的正確密碼。所以,我寫了下面的代碼,但它總是說「沒有登錄」。任何幫助?在Inno Setup中驗證用戶的密碼

var 
    DomainName,UserName,BackwardSlashString,DomainUserName : String; 
    ServerDetailsInputPage : TInputQueryWizardPage; 
    hToken, LoginOk : INTEGER; 

function LogonUser(lpszUsername,lpszDomain,lpszPassword: string; 
    dwLogonType,dwLogonProvider: INTEGER; var hToken: INTEGER): INTEGER; 
external '[email protected] stdcall'; 

procedure InitializeWizard(); 
begin 
    DomainName:= ExpandConstant(GetEnv('USERDOMAIN')); 
    UserName := ExpandConstant(+GetUserNameString); 
    BackwardSlashString := '\' 
    DomainUserName := DomainName + BackwardSlashString + UserName; 
    ServerDetailsInputPage := 
     CreateInputQueryPage(wpWelcome,'','','Please enter following data and click Next.'); 
    ServerDetailsInputPage.Add('IP Address',False); 
    ServerDetailsInputPage.Add('Port Number',False); 
    ServerDetailsInputPage.Add('Domain Name\User Name',False); 
    ServerDetailsInputPage.Add('Password',True); 
    ServerDetailsInputPage.Values[1] := '80'; 
    ServerDetailsInputPage.Values[2] := DomainUserName; 
end; 

function RunAsUser(): BOOLEAN; 
var 
    Passwd : String; 
begin 
    DomainName := ExpandConstant(GetEnv('USERDOMAIN')); 
    UserName := ExpandConstant(+GetUserNameString); 
    Passwd := ServerDetailsInputPage.Values[3]; 
    LoginOk := LogonUser(UserName,DomainName,Passwd,1,0,hToken); 
    if (not (LoginOk=0)) then 
    begin 
     MsgBox('successfully logged-in', mbInformation, MB_OK); 
     Result := true; 
    end 

    else if (LoginOk=0) then 
    begin 
     MsgBox('Not logged-in', mbInformation, MB_OK); 
     Result := false; 
    end; 
end; 

function NextButtonClick(CurPageID: Integer): Boolean; 
begin 
    Result := True; 

    if CurPageID = ServerDetailsInputPage.ID then 
    begin 
     if not RunAsUser then 
     begin 
      MsgBox('Please enter correct Password!', mbError, MB_OK); 
      Result:=False; 
     end; 
    end; 
end; 

function InitializeSetup(): Boolean; 
var 
    PrevInstallPath : String; 
    ResultCode : Integer; 
    FXStopStatus : Boolean; 
begin 
    Result:=True; 
end; 
+4

這沒有任何意義。用戶已經提供了一個有效的密碼,需要登錄到Windows並讓您的安裝程序運行在第一位。要求輸入用戶密碼的安裝程序設置了許多警鈴,確保取消按鈕完美無缺。 – 2013-03-19 12:01:29

+0

接受答案,你可以發佈答案。 – user1752602 2013-03-20 06:08:58

+0

完成。我已經修改了你的腳本並添加了「Domain \ UserName」編輯字段輸入的解析器。如果登錄函數成功,那麼'TryLogonUser'函數返回True,否則返回False。它還返回更重要的'ErrorCode'參數,它指示發生了什麼錯誤(如果有的話)。如果你得到了'ERROR_LOGON_FAILURE',那麼用戶名或密碼是錯誤的,如果'ERROR_SUCCESS'登錄成功(返回True表示什麼)。如果發生不同的錯誤,登錄時出現其他錯誤。 – TLama 2013-03-20 11:00:39

回答

5

起初,你LogonUser函數原型是錯誤的,以及它的呼叫。您不能混合函數原型的數據類型,也不能在函數調用中使用任意值。你可以用這樣的東西來代替:

[Code] 
#ifdef UNICODE 
    #define AW "W" 
#else 
    #define AW "A" 
#endif 
const 
    LOGON32_LOGON_INTERACTIVE = 2; 
    LOGON32_LOGON_NETWORK = 3; 
    LOGON32_LOGON_BATCH = 4; 
    LOGON32_LOGON_SERVICE = 5; 
    LOGON32_LOGON_UNLOCK = 7; 
    LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
    LOGON32_LOGON_NEW_CREDENTIALS = 9; 

    LOGON32_PROVIDER_DEFAULT = 0; 
    LOGON32_PROVIDER_WINNT40 = 2; 
    LOGON32_PROVIDER_WINNT50 = 3; 

    ERROR_SUCCESS = 0; 
    ERROR_LOGON_FAILURE = 1326; 

function LogonUser(lpszUsername, lpszDomain, lpszPassword: string; 
    dwLogonType, dwLogonProvider: DWORD; var phToken: THandle): BOOL; 
    external 'LogonUser{#AW}@advapi32.dll stdcall'; 

var 
    ServerDetailsPage: TInputQueryWizardPage; 

function TryLogonUser(const Domain, UserName, Password: string; 
    var ErrorCode: Longint): Boolean; 
var 
    Token: THandle; 
begin 
    Result := LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE, 
    LOGON32_PROVIDER_DEFAULT, Token); 
    ErrorCode := DLLGetLastError; 
end; 

procedure InitializeWizard; 
var 
    UserName: string; 
begin 
    UserName := AddBackslash(GetEnv('USERDOMAIN')) + GetUserNameString; 
    ServerDetailsPage := CreateInputQueryPage(wpWelcome, 
    '', '', 'Please enter following data and click Next.'); 
    ServerDetailsPage.Add('IP Address', False); 
    ServerDetailsPage.Add('Port Number', False); 
    ServerDetailsPage.Add('Domain Name\User Name', False); 
    ServerDetailsPage.Add('Password', True); 
    ServerDetailsPage.Values[1] := '80'; 
    ServerDetailsPage.Values[2] := UserName; 
end; 

procedure ParseDomainUserName(const Value: string; var Domain, 
    UserName: string); 
var 
    DelimPos: Integer; 
begin 
    DelimPos := Pos('\', Value); 
    if DelimPos = 0 then 
    begin 
    Domain := '.'; 
    UserName := Value; 
    end 
    else 
    begin 
    Domain := Copy(Value, 1, DelimPos - 1); 
    UserName := Copy(Value, DelimPos + 1, MaxInt); 
    end; 
end; 

function ServerDetailsLogonUser: Boolean; 
var 
    Domain: string; 
    UserName: string; 
    Password: string; 
    ErrorCode: Longint; 
begin 
    ParseDomainUserName(ServerDetailsPage.Values[2], Domain, UserName); 
    Password := ServerDetailsPage.Values[3]; 
    Result := TryLogonUser(Domain, UserName, Password, ErrorCode); 

    case ErrorCode of 
    ERROR_SUCCESS: 
     MsgBox('Logon successful!', mbInformation, MB_OK); 
    ERROR_LOGON_FAILURE: 
     MsgBox('The user name or password is incorrect!', mbError, MB_OK); 
    else 
    MsgBox('Login failed!' + #13#10 + SysErrorMessage(DLLGetLastError), 
     mbError, MB_OK); 
    end; 
end; 

function NextButtonClick(CurPageID: Integer): Boolean; 
begin 
    Result := True; 

    if CurPageID = ServerDetailsPage.ID then 
    Result := ServerDetailsLogonUser; 
end; 
+1

感謝您發佈答案。代碼工作正常。 – user1752602 2013-03-21 04:23:03

+0

不客氣! – TLama 2013-03-21 04:27:11

+1

非常感謝! – LEo 2014-12-05 06:49:39