Authentifizierung klappt nicht

cancel
Showing results for 
Search instead for 
Did you mean: 
martinn
Member II

Authentifizierung klappt nicht

Hallo,
vielleicht übersehe ich was oder habe es nicht richtig verstanden. Wenn ich diese Seite ansehe: http://localhost:8080/alfresco/service/index.html, dort auf "Browse 'Authentication' Web Scripts" klicke,
erhalte ich eine Beschreibung für ein Login:
Login (POST)
POST /alfresco/service/api/login

Login and establish a ticket.
Input
JSON Data Object.

username
    cleartext username
password
    cleartext password

Returns the new authentication ticket.

Folgend dieser Beschreibung müsste dieser Code eigentlich funktionieren:


  strUri = "http://localhost:8080/alfresco/service/api/login";
  strJson = @"[""username"", ""admin"", ""password"", ""contacts""]";
  // Create the Web Request Object
  WebRequest request = WebRequest.Create(strUri);
  // Specify that you want to POST data
  request.Method = "POST";
  request.ContentType = "application/x-www-form-urlencoded";
  if (strUri != null)
  {
     // write out the data to the web server
     writeToURL(request, strJson);
  }
  else
  {
     request.ContentLength = 0;
  }
  // read the response from the Web Server
  strHtmlContent = retrieveFromURL(request); //Das Lesen der Antwort ergibt einen Fehler
…. 
 


   private String retrieveFromURL(WebRequest request)
   {
    // 1. Get the Web Response Object from the request
    WebResponse response = request.GetResponse();
    // 2. Get the Stream Object from the response
    Stream responseStream = response.GetResponseStream();
   
    // 3. Create a stream reader and associate it with the stream object
    StreamReader reader = new StreamReader(responseStream);
   
    // 4. read the entire stream
    return reader.ReadToEnd();
   }// end retrieveFromURL method

Leider gibt es hier immer den Fehler 404, 'Bad request'  sobald ich versuche die Antwort zu lesen. Der Fehler tritt auf in der Funktion retrieveFromURL in der ersten Zeile.
Ich bin sicher, das die Funktion retrieveFromURL korrekt funktioniert, denn wenn ich ein Login mit einer GET Funktion aufrufe und die Antwort lese, ist alles ok.
Allerdings sollte das auch mit POST so funktionieren. Weiß jemand Rat?
Danke im Voraus.
3 Replies
jpfi_4454
Member II

Re: Authentifizierung klappt nicht

Hi,
ich bin kein c# experte (ist doch c# oder?), aber du setzt anscheinend den falschen Content-Type.

//anstatt strJson = @"[""username"", ""admin"", ""password"", ""contacts""]";
strJson = "{username: \"admin\", password: \"contact\"}"; //wenn contacts dein PW ist
//anstatt request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/json";
VG, Jan
jpfi_4454
Member II

Re: Authentifizierung klappt nicht

Hi,
sowas kann man immer am schnellsten per cUrl (http://curl.haxx.se/download.html) testen:
In der Shell oder cmd.exe
curl http://localhost:8080/alfresco/service/api/login -H "content-type: application/json" -d "{username: \"admin\", password: \"contacts\"}"
–> sowas wie

{
        "data":
        {
                "ticket":"TICKET_6c3ba5ca3b72872b7ddbe8bcde9cca86f36af08c"
        }
}

VG, jan
martinn
Member II

Re: Authentifizierung klappt nicht

Auch hier vielen Dank, nun hab ich das auch hin bekommen!