javascriptasp.netopenlayersgeoserver

Authenticate in GeoServer with ASP.NET and IIS


I don't know if this is a dumb question, but how can I combine the ASP.NET authentication with OpenLayers?

I created a Login page to authenticate in OpenLayers (in C#, server side), this is my code

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);
}

I don't know if this is the right approach to resolve my problem, anyway if I reach my goal (authenticate with username and password in GeoServer), how can I combine this authentication with OpenLayers, which is in user side (JavaScript)

Thanks in advance.


Solution

  • To authenticate with the GeoSerever from JavaScript side, you must to make a post to the GeoServer autentication servlet(j_spring_security_check). Try use this login function:

         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();
            }
        });
    };
    

    And use this way

     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!");
        }
    });