Hello,
I work on an application and I would like to incorporate Power BI reports. To do this, I must authenticate with Azure AD before any manipulation. The code below allows me to do it, except that I first need to go through the Azure login page. What I would like
to do is to authenticate without going through the login page (in a totally transparent way for the user, directly in the code if possible). Here is the code I use.
Your help will be of great use to me
public ActionResult Index()
{
try
{
//Test for AuthenticationResult
if (Session["authResult"] != null)
{
//Get the authentication result from the session
authResult = (AuthenticationResult)Session["authResult"];
ViewData["token"] = authResult.AccessToken;
ViewData["reportUri"] = Properties.Settings.Default.reportUri;
//GetReports();
}
else
{
//Connexion à Azure AD
SignInAzureAD();
}
}
catch (Exception ex)
{
throw ex;
}
return View();
}
public void SignInAzureAD()
{
//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", Properties.Settings.Default.Resource},
//After user authenticates, Azure AD will redirect back to the web app
{"redirect_uri", Properties.Settings.Default.RedirectUrl},
};
//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
var authUri = String.Format("{0}?{1}", Properties.Settings.Default.authorityUri, queryString);
Response.Redirect(authUri);
}
public ActionResult Redirect()
{
//Redirect uri must match the redirect_uri used when requesting Authorization code.
// 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(Properties.Settings.Default.authorityUri, TC);
ClientCredential cc = new ClientCredential
(Properties.Settings.Default.ClientID,
Properties.Settings.Default.ClientSecret);
AuthenticationResult AR = AC.AcquireTokenByAuthorizationCode(code, new Uri(Properties.Settings.Default.RedirectUrl), cc);
//Set Session "authResult" index string to the AuthenticationResult
Session["authResult"] = AR;
//Redirect back to Default.aspx
Response.Redirect("/Dashboard/Index");
return View("Index");
}
CRM_USER