2012-06-08 3 views
3

이것이 바보 같은 질문인지는 모르겠지만 어떻게 asp.net 인증을 openlayers와 "결합"할 수 있습니까? 내가 (C#을, 서버 측에서) openlayers에 인증하기 위해 로그인 페이지를 만들었 asp.net 및 iis로 geoserver에서 인증

이이 내 문제를 해결하기 위해 올바른 접근 방식 인 경우 어쨌든 경우, 모르는 내 코드

Uri uri = new Uri("http://"+username+":"+password+"@localhost:1979/geoserver/wms"); 
     if (uri.Scheme == Uri.UriSchemeHttp) 
     { 
      HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
      request.Method = WebRequestMethods.Http.Post; 

      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      StreamReader reader = new StreamReader(response.GetResponseStream()); string tmp = reader.ReadToEnd(); 
      response.Close(); 
      Response.Write(tmp); 
     } 

입니다 나는 사용자 측에 내가 openlayers로이 인증을 결합 할 수있는 방법을 내 목표 (geoserver에서 사용자 이름 및 암호 인증), (자바 스크립트) 자바 스크립트 측면에서 geoserever 인증을 사전에

답변

4

에서

감사에 도달 , 너는해야한다. geoserver autentication 서블릿에 대한 게시물 (j_spring_security_check). 이 로그인 기능을 사용하여보십시오 :

 function login (options) { 
    // url del servlet del geoserver 
    var url = options.server + "/geoserver/j_spring_security_check"; 
    // parametros para el login 
    params = "username=" + options["user"] + "&password=" 
       + options["password"]; 

    var contentType = "application/x-www-form-urlencoded"; 
    //se inicializa la petición ajax 
    var ajax = $.ajax({ 
     data : params, 
     type : "POST", 
     contentType : contentType, 
     url : url 
    }); 
    // se ejecuta cuando la peticion finaliza 
    ajax.done(function() { 

     if ($.cookie("JSESSIONID") != null && options && options.success) { 
      options.success(); 
     } 
    }); 
    // si ocurrio un error al realizar la peticion 
    ajax.fail(function(data) { 
     if (options && options.failure) { 
      options.failure(data); 
     } 
    }); 
    // se ejecuta siempre al final de la petición, sin importar que esta 
    // haya fallado 
    ajax.always(function() { 
     if (options && options.always) { 
      options.always(); 
     } 
    }); 
}; 

을 그리고 GeoServer가 제대로 2.7 후 인증 통과를 처리하지 않습니다 어떤 이유로이 방법

login({ 
    user:"admin" //geoserver user 
    password: "adminPassword", 
    server : "http://192.168.10.1:8080", //geoserver host 
    success : function(){ 
     alert("Login OK!"); 
    }, 
    failure : function(){ 
     alert("Login fail!"); 
    } 
}); 
+0

nice! 이 고마워 :) –

0

를 사용합니다. 그래서, 서버의 페이지 메소드에서 로그인하고 auth 쿠키를 세션 변수에 저장하는 방법을 추가했습니다. 예쁘지는 않지만 작동합니다.

  'Post a login request to GS. 
     Dim myCookies As String = "" 
     Dim Method As String = "POST" 

     Dim HttpWRequest As System.Net.HttpWebRequest = CType(WebRequest.Create("https://" & GSServer & "/geoserver/j_spring_security_check"), HttpWebRequest) 

     Dim PostData As String = "username=" + UID + "&password=" + PWD 
     HttpWRequest.KeepAlive = False 
     HttpWRequest.Headers.Set("Pragma", "no-cache") 
     'HttpWRequest.Headers.Set("Translate", "f") 
     HttpWRequest.ContentType = "text/xml" 
     HttpWRequest.ContentLength = PostData.Length 

     'This is important, otherwise j_spring security check does an immediate redirect, and the cookie is lost. 
     HttpWRequest.AllowAutoRedirect = False 

     If String.IsNullOrEmpty(UID) = False Then 
      HttpWRequest.Credentials = New NetworkCredential(UID, PWD) 
     End If 

     If String.IsNullOrEmpty(myCookies) = False Then 
      HttpWRequest.Headers("Cookie") = myCookies 
     End If 

     'Set the request timeout to 5 minutes. 
     HttpWRequest.Timeout = 300000 
     ' set the request method 
     HttpWRequest.Method = Method 

     If Method = "POST" Or Method = "PUT" Then 
      ' Store the data in a byte array. 
      Dim ByteQuery() As Byte = System.Text.Encoding.ASCII.GetBytes(PostData) 
      HttpWRequest.ContentLength = ByteQuery.Length 
      Dim QueryStream As Stream = Nothing 
      Try 
       QueryStream = HttpWRequest.GetRequestStream() 
      Catch e As Exception 
       'WriteToLog(e.Message) 
       Throw New ArgumentException("Couldn't log into GeoServer.") 
      End Try 
      ' Write the data to be posted to the Request stream. 
      QueryStream.Write(ByteQuery, 0, ByteQuery.Length) 
      QueryStream.Close() 
     End If 

     ' Send the request and get a response. 

     Dim HttpWResponse As HttpWebResponse 
     Try 
      HttpWResponse = HttpWRequest.GetResponse() 
     Catch e As Exception 
      'WriteToLog(e.Message) 
      Throw New ArgumentException("Couldn't log into GeoServer.") 
     End Try 

     ' Get the Response stream. 
     Dim strm As Stream = HttpWResponse.GetResponseStream() 

     ' Read the Response stream. 
     Dim sr As StreamReader = New StreamReader(strm) 
     Dim sText As String = sr.ReadToEnd() 



     ' Close the stream. 
     strm.Close() 

     myCookies = HttpWResponse.GetResponseHeader("Set-Cookie") 
     HttpContext.Current.Session("GeoServerSessionID") = myCookies 
     ' Clean up. 
     HttpWRequest = Nothing 
     HttpWResponse = Nothing 

     If sText.Contains("Invalid username/password combination") Or String.IsNullOrEmpty(myCookies) Then 
      Throw New ArgumentException("Couldn't log into GeoServer.") 
     End If