I have server-side VB code to retrieve an access_token and save it in the session. I want to make the token available client-side so I can use it to send an ajax request.
I tried to set the token in a hidden field. This works. The token shows in the field when I check via the browser dev tools.
I then tried to get the hidden field value in javascript with getHiddenValue(). This does not work. getHiddenVaue() does not execute. But when funciones.RedirigiraUsuario() is commented out, getHiddenValues() works.
RedirigiraUsuario() has a switch case to redirect a view depending on the profile
The vb code to set the token looks like this:
Dim url As String = funciones.getPropiedad(Me.Page, "urlWebApi") &
"recuperarToken"
Dim data As New NameValueCollection()
data.Set("grant_type", "password")
data.Set("username", funciones.reemplaza_caracteres(txt_login.Text))
If ViewState("ESADMIN") Then
data.Set("password", NegUsuario.get_GEN_claveUsuarios())
Else
data.Set("password", contraseña)
End If
data.Set("clientid", "2")
'data.Set("idperfil", "63")
Dim sJson As String = funciones.ObtenerJsonWebApi(url, data)
If Not sJson = Nothing Then
Dim json As Dictionary(Of String, String) = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(sJson)
Session("TOKEN") = "Bearer " + json("access_token")
'setting the token in hidden field
inputToken.Value = "Bearer " + json("access_token")
'here is the problem, this does not works, maybe the postback is the guilty
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ShowStatus", "javascript: getHiddenValue();", True)
End If
funciones.RedirigiraUsuario(Me)
Javascript code :
<script type="text/javascript">
$(document).ready(function () {
$('#div_login').hide();
$('#div_login').fadeIn(1500);
document.getElementById('div_login').scrollIntoView();
});
function incorrecto() {
$("#div_login").addClass('go');
}
function getHiddenValue() {
console.error("asdasdasdasda")
let hdnField = document.getElementById('<%= inputToken.ClientID %>').value;
showNotificacion("asdas",'info', 'center', 'top',200)
localStorage.setItem("PabToken", hdnField)
sessionStorage.setItem("PabToken", hdnField)
return true
}
</script>
How can I execute the getHiddenValue() function before postback, or get the token saved in the server-side?
Edit
Public Shared Sub RedirigiraUsuario(ByRef pagina As Page)
Select Case pagina.Session("PERFIL")
Case "1", "2", "3", "4", "5", "N"
pagina.Response.Redirect("~/Default.aspx")
Case "6"
pagina.Response.Redirect("~/FormMantenedor/MAN_JefeEspecialidad.aspx")
Case "7"
pagina.Response.Redirect("~/BUS_General.aspx")
Case "8"
pagina.Response.Redirect("~/FormPabellon/GST_Detalle_Post_Anestesia.aspx")
Case "9"
pagina.Response.Redirect("~/FormTabla/ING_Tabla.aspx")
Case "10", "11", "12", "15"
pagina.Response.Redirect("~/FormPreTabla/GST_Pre_Tabla.aspx")
Case "14"
pagina.Response.Redirect("~/GST_Camas.aspx")
Case "13"
pagina.Response.Redirect("~/FormPabellon/REP_Pabellon.aspx")
End Select
End Sub
ClientIDMode = "static"to theinputTokencontrol, then it will use the ID without changing it, so you could uselet hdnField = document.getElementById('yourChosenID').value;instead. Control.ClientIDMode Property