目前,我首先从 api/tokens 端点获取 authToken。将窗口位置更改为 https://{guacServer}/#client/{connectionId}?token={authToken}。
但是现在我想像这个 PR 一样将令牌作为标头传递(https://github.com/apache/guacamole-client/pull/649)。
我尝试在 XMLHttp 请求的标头中传递令牌,但它为 api/token 提供 403 错误。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guacamole Authorization - My ASP.NET Application</title>
</head>
<body>
<h3>Verification is in progress, kindly wait </h3>
<script src="jquery.js"></script>
<script type="text/javascript">
function getQueryVariable( variable )
{
let query = window.location.search.substring( 1 );
let vars = query.split( "&" );
for ( let i = 0; i < vars.length; i++ )
{
let pair = vars[i].split( "=" );
if ( pair[0] == variable ) { return pair[1]; }
}
return ( false );
}
$( document ).ready( function ()
{
let guacServer = decodeURIComponent( getQueryVariable( "guacserver" ) );
let connectionId = decodeURIComponent( getQueryVariable( "connectionId" ) );
let connectionData = decodeURIComponent( getQueryVariable( "connectionData" ) );
let settings = {
"url": guacServer + "/api/tokens",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"data": connectionData
}
};
$.ajax( settings ).done( function ( authData )
{
console.log( authData );
let authToken = authData.authToken;
//window.location.href = guacServer + '/#/client/' + connectionId + '?token=' + authToken; // this works
let url = guacServer + "/#/client/" + connectionId;
let req = new XMLHttpRequest();
req.open( 'GET', url );
req.setRequestHeader( 'Guacamole-Token', authToken )
req.setRequestHeader( 'Content-Type', "application/json" )
req.onload = function ()
{
document.open()
document.write( req.responseText )
window.location.href = url
document.close()
}
req.send();
} ).fail( function ( data )
{
console.log( "error data: " + data );
alert( "Failed to authorize. Either token is expired or connection parameter is corrupt" )
} );
} );
</script>
</body>
</html>