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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
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
Employee
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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