Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
rishabhshukla12
New Member

PBIWebAPP C# Refresh Token

Is there anyone who could tell me how to use the refresh token to get a new token. I am using PBIWebAPP dashboard code available on GitHub. I am a beginner in C# and don't know much about the how to use AuthenticationContext.AcquireTokenByRefreshToken.

 

The code I am using to generate the access token, which is getting expired in 1 hours. Can anyone help to put refresh token method so that I don't have to manually run this code again and again to get the new access token. Also, how can I extend the expiry time of the token?

 

 

protected void Page_Load(object sender, EventArgs e)
{
//Redirect uri must match the redirect_uri used when requesting Authorization code.
string redirectUri = String.Format("{0}Redirect", Properties.Settings.Default.RedirectUrl);
string authorityUri = Properties.Settings.Default.AADAuthorityUri;

// Get the auth code
string code = Request.Params.GetValues(0)[0];

// Get auth token from auth code
TokenCache TC = new TokenCache();

AuthenticationContext AC = new AuthenticationContext(authorityUri, TC);
ClientCredential cc = new ClientCredential
(Properties.Settings.Default.ClientID,
Properties.Settings.Default.ClientSecret);

 AuthenticationResult AR = AC.AcquireTokenByAuthorizationCode(code, new Uri(redirectUri), cc);

//Set Session "authResult" index string to the AuthenticationResult
Session[_Default.authResultString] = AR;

//Redirect back to Default.aspx
Response.Redirect("/Default.aspx");
}

 

Thanks.

2 REPLIES 2
Eric_Zhang
Microsoft Employee
Microsoft Employee

@rishabhshukla12

Regarding extend the liftertime of access token, it is an Azure AD question, to get a bettere response, I'd suggest your post in the dedicated AAD forum. Just for your reference, Configurable token lifetimes in Azure Active Directory (Public Preview).

 

As to how to get accesstoken with refresh token, you could reference below sample. It is for demo purpose, you don't have to get a new refreshtoken every time, the refreshtoken by default lives for 14 days, according to the above link.

Also, please note the the refreshtoken and AcquireTokenByRefreshToken seems no longer exposed in ADAL 3.0 or newer.  For further questions, please go to the AAD forum as well.

 

 

        public string GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
        {
            //Redirect uri must match the redirect_uri used when requesting Authorization code.
            //Note: If you use a redirect back to Default, as in this sample, you need to add a forward slash
            //such as http://localhost:13526/

            // Get auth token from auth code       
            TokenCache TC = new TokenCache();

            //Values are hard-coded for sample purposes
            string authority = Properties.Settings.Default.AADAuthorityUri;
            AuthenticationContext AC = new AuthenticationContext(authority, TC);
            ClientCredential cc = new ClientCredential(clientID, clientSecret);

            //Set token from authentication result

            string refreshtoken = AC.AcquireTokenByAuthorizationCode(
                 authorizationCode,
                 new Uri(redirectUri), cc).RefreshToken;

           return AC.AcquireTokenByRefreshToken(refreshtoke, cc, "https://analysis.windows.net/powerbi/api").AccessToken; 
        }

 

What Microsoft.IdentityModel.Clients.ActiveDirectory version are you using?

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.