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;
}
}
...
}







