ASP.Net: Get login details stored in Session using JavaScript

divyacheeradath
 
on Jan 24, 2023 09:49 PM
588 Views

Each page of application have to check if the system is idle for 10 minutes, and if then have to refresh the oAuth access token with new refresh token. I have done like below. But I don't know how to get the username and refresh token stored in GlobalVariableclass in ajax.  In code behind of aspx I used to get it as below.

 GlobalVariables obj_GlobalVariables = (GlobalVariables)HttpContext.Current.Session["objGlobalVariableClass"];
 string refesh_token=obj_GlobalVariables.login_refresh_token;

Below is my Global variable class. When login success, stores the login details in GlobalVariables class as below

public class GlobalVariables
{
    public  int login_user_role = 0;
    public  string login_user_name = string.Empty;
    public  string login_user_id = string.Empty;
    public  string login_token = string.Empty;
    public string login_refresh_token = string.Empty;
    public int login_token_expires_in = 0;//1799 sec; 29.9833 minute//1799000
}
GlobalVariables obj_GlobalVariables = new GlobalVariables();
obj_GlobalVariables.login_token = result.access_token;
obj_GlobalVariables.login_token_expires_in = Convert.ToInt32(result.expires_in*1000);//seconds to millisec
obj_GlobalVariables.login_refresh_token=result.refresh_token;
obj_GlobalVariables.login_user_name =result.login_user_name;

Below is my javascript. I want to get login_refrsh_token and login_user_name from GlobalVaribaleclass here.

<script type="text/javascript">
        var interval;
        var IdealTimeOut = 0;
        var idleSecondsTimer = null;
        var idleSecondsCounter = 0;
        var current_time_in_seconds = 0;
        document.onclick = function () { idleSecondsCounter = 0; };
        document.onmousemove = function () { idleSecondsCounter = 0; };
        document.onkeypress = function () { idleSecondsCounter = 0; };

        /*have to get below two values from global class.*/
        var username = null;
        var refresh_token = null;

        function SessionExpireAlert() {
        
            IdealTimeOut = 10;
            idleSecondsTimer = window.setInterval(CheckIdleTime, 1000);
        };
        function CheckIdleTime() {
            idleSecondsCounter++;

             current_time_in_seconds = new Date().getTime() / 1000;
             if (idleSecondsCounter >= IdealTimeOut) {
              window.clearInterval(idleSecondsTimer);
                callAJAXrefreshToken();
            }
        }
            
        function callAJAXrefreshToken() {
            $.ajax({
                type: "POST",
                url: "webservice.aspx/GetLoginTokenWithRefreshToken",
                data: "{ username: '" + username + "', refresh_token: " + refresh_token + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
 
                },
                error: function (r) {
                    alert(r.responseText);
                },
                failure: function (r) {
                    alert(r.responseText);
                }
            });
            return false;
        }
</script>
Download FREE API for Word, Excel and PDF in ASP.Net: Download
PrinceG
 
on Jan 24, 2023 11:47 PM

Hi divyacheerada,

Please refer below article link.

ASP.Net: Get HttpContext.Current.Session value in JavaScript or jQuery