Session value returns "null" ?

Hi there,

I have the following code and I am trying to get a token from session cache but it returns null. 

public static string GetSessionToken()
        {
            var session = HttpContext.Current.Session;
            if (session != null)
            {
                var startTokenKey = "_TokenCache";
                string ServiceResourceId = "https://outlook.office365.com/";
                var tokenKey = session.Keys
                                .Cast<string>()
                                .Where(k => k.Contains(startTokenKey))
                                .SingleOrDefault();

                if (tokenKey != null)
                {
                    var tokenObject = session[tokenKey + ServiceResourceId];
                    //Hack to get value since CacheItem type isn't accessible
                    if (tokenObject != null)
                    {
                        var token = tokenObject.GetType().GetField("Value").GetValue(tokenObject).ToString();
                        return token;
                    }
                }
            }
            return null;
        }

I have no idea about session at all, why the above function always return null ? where did I go wrong ? Please help me figure out the problem so that I can rectify the error. I am completely clueless here.

It’s returning null because the info it is looking for isn’t in the session.  The problem is unlikely to be the code you posted but to do with how and when you add the data to the session which you haven’t explained.

SharpDragon

1) I am trying to get a token from session cache
2) but it returns null

Probably you should see what exactly returns null as there are multiple cases when you could get null

1) session is null (in this case your method does nothing and returns null)
2) session is not null but it either has no key with a name containg "_TokenCache"
3) session is not null and key with "_TokenCache" is there but there is no key with name tokenKey + ServiceResourceId or its value is null
4) all above is not null but .GetValue(tokenObject) is null

Hi , Good morning.

I am using Active Directory (Adal.net) to connect to office 365 Api via Oauth2.0.  You can see I am successfully receiving the token. Here is the IIS Log:

iisexpress.exe Information: 0 : ADAL .NET with assembly version '2.12.0.0', file version '2.12.11107.1459' and informational version '0adb54f5edd13d1866d15c6d445bb08081e85957' is running...
iisexpress.exe Information: 0 : === Token Acquisition started. Correlation ID: 8fbedf41-d8fb-430f-93da-0c3b2e9d7589
iisexpress.exe Information: 0 : Access Token with hash '6PD59RQZC0z8L74x2ygf5iNnFdp9EwDuQrqLUy4OKw8=' and Refresh Token with hash 'LDI3/rtqY1olt0rv5LKfPe/GQukVf21Yv+N6j5eMzNo=' returned. Correlation ID: 8fbedf41-d8fb-430f-93da-0c3b2e9d7589
iisexpress.exe Information: 0 : === Token Acquisition started. Correlation ID: 9972d971-92bb-4ee3-a68d-9cd776a5e44e
iisexpress.exe Information: 0 : A matching token was found in the cache. Correlation ID: 9972d971-92bb-4ee3-a68d-9cd776a5e44e
iisexpress.exe Information: 0 : Access Token with hash 'nssYffUW/6466ukfECC+yeT7LmEKDTi8kzNGyfq7tBo=' and Refresh Token with hash 'EhgZuCWdI/2kYsvtsquTtDeA95cUug0dvQTdQNRhi3g=' returned. Correlation ID: 9972d971-92bb-4ee3-a68d-9cd776a5e44e
iisexpress.exe Information: 0 : === Token Acquisition started. Correlation ID: b975cf29-0e5f-4ac7-be58-381e5112182e
iisexpress.exe Information: 0 : A matching token was found in the cache. Correlation ID: b975cf29-0e5f-4ac7-be58-381e5112182e
iisexpress.exe Information: 0 : Access Token with hash '3/nI4yuYE98VGdV86kKaHSpVW925MbIs8h2qUHMeydA=' and Refresh Token with hash 'UQ7NZjFGDswbhPM4gCwu7LulRvigc+JSKNuBWpKJhaA=' returned. Correlation ID: b975cf29-0e5f-4ac7-be58-381e5112182e

I checked the values of tokenkey and tokenObject in my code.

tokenkey = > _TokenCache

tokenObject >= null

Maybe _TokenCache is not the correct key I am using. I am not sure. :(

Well, it’s stil unclear.

SharpDragon

tokenkey = > _TokenCache

tokenObject >= null

What does => stand for?

If tokenkey is not null but tokenObject is null then you need to check

var tokenObject = session[tokenKey + ServiceResourceId];

because this is the only line where you get tokenObject.

SharpDragon

Maybe _TokenCache is not the correct key I am using. I am not sure.

Maybe, who knows? :-)

Are you sure that tokenkey is not null?

My service resource ID is correct.  Please tell me how to loop through session keys here and make sure I am using correct session key as "startTokenkey".  

Yes, tokenkey also returns null, sorry for inconvenience. 

I think you do not understand the code.

If tokenkey is null, then in this code

var tokenKey = session.Keys
         .Cast<string>()
         .Where(k => k.Contains(startTokenKey))
         .SingleOrDefault();

a) either session is null 
b) or condition is not correct.

Are you sure that session is NOT null? You can check it e.g. 

if (session != null)
{
Response.Write("SESSION IS NOT NULL");
...
} else {

Response.Write("SESSION IS NULL");
}

I just checked, it says "Session is not null".

Good, it means that there is no key which has a name with "_TokenCache" in it.

Are you sure that value you search is "_TokenCache" and not ‘LDI3/rtqY1olt0rv5LKfPe/GQukVf21Yv+N6j5eMzNo=’ or something like this?

The problem is I did not search the value ,  I followed this article :

http://www.jeremythake.com/2014/08/using-the-exchange-online-ews-api-with-office-365-api-via-azure-ad/

I copied the above code from this article. Would you please tell me how to find out the correct key/value which stores the accesstoken ?

I think you need to read it again because in the original code it has

var startTokenKey = "_O365#AccessToken#";

I suggest you close the topic now as it neither related to ASP.NET nor MVC

I know.. I tried this original value already.  ok. I close this topic. 

best regards,

Hi,

I don’t think that value stored in the session. That value may store in the cookie or URL. So you need to know where that data stored.

Best Regards

Starain

Leave a Reply