How to get values out of SessionTokenStore in Controller and View

Joe Green 146 Reputation points
2023-02-22T17:08:20.52+00:00

In asp.net 4.8 MVC application, I've used MS Graph to get access token as shown below in Startup.Auth.cs. How do I access values stored in tokenStore in Controller and View. I'm not sure how to assign value from tokenStore to ViewBag.DIsplayName in controller.


var signedInUser = new ClaimsPrincipal(notification.AuthenticationTicket.Identity);
var tokenStore = new SessionTokenStore(confidentialClientApplication.UserTokenCache, HttpContext.Current, signedInUser);

string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

var result = await confidentialClientApplication.AcquireTokenByAuthorizationCode(
	scopes, notification.Code).ExecuteAsync();

var userDetails = await GraphHelper.GetUserDetailsAsync(result.AccessToken);

tokenStore.SaveUserDetails(userDetails);
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,296 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,713 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 11,656 Reputation points Microsoft External Staff
    2023-02-24T05:55:28.2633333+00:00

    Hi @Joe Green,

    According to your description, I want to confirm whether you are referring to this official example: https://github.com/microsoftgraph/msgraph-training-aspnetmvcapp/tree/main/Demos/03-add-msgraph/graph-tutorial

    If this is the case, as we tested, this demo works normally, and the userDetails has been correctly obtained in BaseController and stored in ViewBag. This is test result:

    User's image

    This is the corresponding logic code ( In method onActionExcuting):

    User's image

    So I'm not sure at which step you have the problem. If possible, please provide more details.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.