다이제스트 액세스 인증을 요청하는 서버로 응답을 보내려고 합니다.
....
FResponseHeader.Text := FResponseText;// received header.
FResponseHeader.ProcessHeaders;
....
WriteLn(FResponseHeader.WWWAuthenticate); //WWW-Authenticate: Digest realm="xxxx.com", nonce="fq1uvocyzvr17e6a5syproazd5phwdvhvlc5", stale=false, algorithm=MD5, qop="auth"
LIdAuthentication := TIdDigestAuthentication.Create;
try
LIdAuthentication.Username := FUser;
LIdAuthentication.Password := FPass;
LIdAuthentication.Uri := FURI;
LIdAuthentication.Method := GetMsgTypeString(FResponseHeader.RequestMethods);
LIdAuthentication.Params.Values['Authorization'] := FResponseHeader.WWWAuthenticate;
LIdAuthentication.AuthParams.AddValue('Digest', FResponseHeader.WWWAuthenticate);
for I := 0 to LIdAuthentication.Steps do
LIdAuthentication.Next;
Result := LIdAuthentication.Authentication;
finally
LIdAuthentication.Free;
end;
서버에서 401 을 받았습니다 .
Authorization Header 를 만드는 올바른 방법은 무엇입니까 ?
TIdDigestAuthentication
(및 기타 TIdAuthentication
파생 클래스)는 TIdHTTP
독립형 이 아닌와 함께 사용하기위한 것 입니다.
를 사용하여 TIdHTTP
서버와 통신하는 경우 다이제스트 인증을 수동으로 관리 할 필요가 전혀 없습니다. 서버가 헤더 Digest
에서 요청 하고 (또는 )이 절 에 있으면 자동으로 Digest 응답을 보냅니다. 자신에게 관심을 가져야 할 유일한 것은 다음과 같습니다.WWW-Authenticate
IdAuthenticationDigest
IdAllAuthentications
uses
TIdHTTP
초기 인증 시도에 대한 TIdHTTP.Request.Username
및 TIdHTTP.Request.Password
속성을 설정합니다 .
TIdHTTP.OnAuthorization
서버가 현재 Username
/를 거부 할 가능성을 처리하도록 이벤트 핸들러를 설정하여 Password
선택적으로 사용자에게 프롬프트 한 후 재 시도에 대한 새 값을 제공 할 수 있습니다.
TIdHTTP.OnSelectProxyAuthorization
서버에서 여러 체계를 요청하는 경우 및 / 또는 다른 체계보다 우선 순위를 갖는 체계를 제어하려는 경우 사용할 인증 체계를 선택 하는 이벤트 처리기를 선택적으로 설정합니다 .
예를 들면 :
uses
..., IdHTTP, IdAuthenticationDigest;
...
IdHTTP1.OnAuthorization := AuthRequested;
IdHTTP1.Request.Username := ...; // initial username
IdHTTP1.Request.Password := ...; // initial password
IdHTTP1.Get(...);
...
procedure TMyClass.AuthRequested(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
if (new credentials are available) then
begin
Authentication.Username := ...; // new username
Authentication.Password := ...; // new password
Handled := True;
end else
Handled := False;
end;
즉, TIdDigestAuthentication
독립 실행 형 을 사용하려면 사용 방법과 유사하게 사용해야 TIdHTTP
합니다. 예 :
LIdAuthentication := TIdDigestAuthentication.Create;
try
LIdAuthentication.SetRequest(FGetMsgTypeString(FResponseHeader.RequestMethods), FURI);
LIdAuthentication.Username := FUser;
LIdAuthentication.Password := FPass;
LIdAuthentication.Params.Values['Authorization'] := LIdAuthentication.Authentication;
LIdAuthentication.AuthParams := FResponseHeader.WWWAuthenticate; // assuming WWWAuthenticate is a TIdHeaderList...
repeat
case LIdAuthentication.Next of
wnAskTheProgram:
begin
// set LIdAuthentication.Username and LIdAuthentication.Password to new credentials to retry...
end;
wnDoRequest:
begin
// send new request with LIdAuthentication.Authentication in the 'Authorization' header...
Result := LIdAuthentication.Authentication;
Exit;
end;
wnFail:
begin
// error handling ...
Result := '';
Exit;
end;
end;
until False;
finally
LIdAuthentication.Free;
end;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다