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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
xuandungpy
Frequent Visitor

AccessToken lifetime from web app

I Register a Web Application and Authenticate a Web Application, AccessToken's Lifetime? Thank you.

 

protected void signInButton_Click(object sender, EventArgs e)
    {
        //Create a query string
        //Create a sign-in NameValueCollection for query string
        var @params = new NameValueCollection
        {
            //Azure AD will return an authorization code.
            //See the Redirect class to see how "code" is used to AcquireTokenByAuthorizationCode
            {"response_type", "code"},

            //Client ID is used by the application to identify themselves to the users that they are requesting permissions from.
            //You get the client id when you register your Azure app.
            {"client_id", Properties.Settings.Default.ClientID},

            //Resource uri to the Power BI resource to be authorized
            {"resource", "https://analysis.windows.net/powerbi/api"},

            //After user authenticates, Azure AD will redirect back to the web app
            {"redirect_uri", "http://localhost:13526/Redirect"}
        };

        //Create sign-in query string
        var queryString = HttpUtility.ParseQueryString(string.Empty);
        queryString.Add(@params);

        //Redirect authority
        //Authority Uri is an Azure resource that takes a client id to get an Access token
        string authorityUri = "https://login.windows.net/common/oauth2/authorize/";
        Response.Redirect(String.Format("{0}?{1}", authorityUri, queryString));       
    }

public partial class Redirect : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
      //Redirect uri must match the redirect_uri used when requesting Authorization code.
      string redirectUri = "http://localhost:13526/Redirect";
      string authorityUri = "https://login.windows.net/common/oauth2/authorize/";

      // 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["authResult"] = AR;

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

public partial class _Default : Page
{
  public AuthenticationResult authResult { get; set; }
  string baseUri = "https://api.powerbi.com/beta/myorg/";

  protected void Page_Load(object sender, EventArgs e)
  {

      //Test for AuthenticationResult
      if (Session["authResult"] != null)
      {
          //Get the authentication result from the session
          authResult = (AuthenticationResult)Session["authResult"];

          //Show Power BI Panel
          signInStatus.Visible = true;

          //Set user and token from authentication result
          userLabel.Text = authResult.UserInfo.DisplayableId;
          accessTokenTextbox.Text = authResult.AccessToken;
      }
  }

  ...
}

 

 

1 ACCEPTED SOLUTION
Eric_Zhang
Microsoft Employee
Microsoft Employee


@xuandungpy wrote:

I Register a Web Application and Authenticate a Web Application, AccessToken's Lifetime? Thank you.


@xuandungpy

By default, the token expires in one hour. You can find the expire time("exp" field) after decoding the token.

Check more Configurable token lifetimes in #AzureAD.

View solution in original post

3 REPLIES 3
izurev
Frequent Visitor

how was this solved?

want to increase the token lifetime to 1 day.

how can I do that?

 

Thanks,

Alex

Supraj
New Member

Hey, Can You Tell how actually you solved from the above sample code.

I want to increase the exp time to 1 day and also want to know how you solved this problem

Eric_Zhang
Microsoft Employee
Microsoft Employee


@xuandungpy wrote:

I Register a Web Application and Authenticate a Web Application, AccessToken's Lifetime? Thank you.


@xuandungpy

By default, the token expires in one hour. You can find the expire time("exp" field) after decoding the token.

Check more Configurable token lifetimes in #AzureAD.

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.