使用 asp.net 和 IIS 在地理服务器中进行身份验证
Authenticate in geoserver with asp.net and iis
我不知道这是否是一个愚蠢的问题,但是如何将 asp.net 身份验证与openlayers"结合"?
我创建了一个登录页面以在 openlayers(在 c# 中,服务器端)进行身份验证,这是我的代码
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 相结合(javascript)
提前致谢
要从 javascript 端向 geoserever 进行身份验证,您必须向地理服务器 autentication servlet(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();
}
});
};
并以此方式使用
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!");
}
});
由于某种原因,GeoServer 在 2.7 之后无法正确处理身份验证直通。因此,我添加了一种从服务器上的页面方法登录并将身份验证cookie保存在会话变量中的方法。不漂亮,但它有效。
'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
相关文章:
- Sencha Touch和远程服务器上的身份验证
- ServiceStack JavaScript服务器事件客户端的身份验证
- 注册,登录和分享内容通过LinkedIn和Twitter与服务器端身份验证(如Google+)
- 来自chrome扩展的服务器端Facebook身份验证
- req.user在使用express服务器passport.js进行twitter身份验证后未定义
- OAuth流通过电话间隙和服务器端对用户进行身份验证
- 向具有不同域和基本身份验证的服务器发出 ajax POST 请求是不可能的
- 使用 asp.net 和 IIS 在地理服务器中进行身份验证
- 如何在Python开发Web服务器上使用实际的Google身份验证
- 使用服务器端身份验证处理Backbone.model.default
- Restify静态Web服务器在启用HTTP基本身份验证后停止工作
- 来自远程服务器的AngularJS身份验证
- ExpressJS和Angular-'留下'应用程序进行服务器端身份验证
- Web身份验证-如何安全地将用户名/密码从客户端传输到服务器
- Ember用户身份验证(客户端和服务器端完全分离)
- php cURL的替代品- OpenId和谷歌,Facebook api服务器端身份验证
- Google OAuth -使用Javascript前端和服务器后端(授权代码流)进行身份验证
- 在进行身份验证时,如果服务器必须始终检查凭据,那么JavaScript验证的意义是什么?
- 正在对Parse.com用户和会话服务器端进行身份验证
- 如何将用户重定向到不同的服务器并包括HTTP基本身份验证凭据