Skip to content

Commit

Permalink
Added further logging when access token is retrieved
Browse files Browse the repository at this point in the history
  • Loading branch information
Yvand committed Aug 24, 2016
1 parent 893991c commit 158f76c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
14 changes: 9 additions & 5 deletions AzureCP/AuthenticationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.Azure.ActiveDirectory.GraphClient;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.SharePoint.Utilities;

namespace azurecp
{
Expand Down Expand Up @@ -38,11 +39,14 @@ public static string GetTokenForApplication(string tenantName, string clientId,
/// <returns>ActiveDirectoryClient for Application.</returns>
public static ActiveDirectoryClient GetActiveDirectoryClientAsApplication(string tenantName, string tenantId, string clientId, string clientSecret)
{
Uri servicePointUri = new Uri(Constants.ResourceUrl);
Uri serviceRoot = new Uri(servicePointUri, tenantId);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
async () => await AcquireTokenAsyncForApplication(tenantName, clientId, clientSecret));
return activeDirectoryClient;
using (new SPMonitoredScope(String.Format("[AzureCP] Getting access token for tenant {0} by connecting to '{1}' ", tenantName, Constants.ResourceUrl), 1000))
{
Uri servicePointUri = new Uri(Constants.ResourceUrl);
Uri serviceRoot = new Uri(servicePointUri, tenantId);
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot,
async () => await AcquireTokenAsyncForApplication(tenantName, clientId, clientSecret));
return activeDirectoryClient;
}
}

///// <summary>
Expand Down
3 changes: 3 additions & 0 deletions AzureCP/AzureCP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ private List<AzurecpResult> QueryAzureAD(AzureTenant coco, Expression<Func<IUser
return null;
}
coco.ADClient = activeDirectoryClient;
LogToULS(String.Format("[{0}] Got new access token for tenant '{1}'", ProviderInternalName, coco.TenantName), TraceSeverity.Medium, EventSeverity.Information, AzureCPLogging.Categories.Lookup);
//activeDirectoryClient.Oauth2PermissionGrants.
//activeDirectoryClient.Oauth2PermissionGrants.Context.
}

List<AzurecpResult> allADResults = new List<AzurecpResult>();
Expand Down
10 changes: 7 additions & 3 deletions AzureCP/AzureCP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.SharePoint" />
<Reference Include="Microsoft.SharePoint.Client.ServerRuntime" />
<Reference Include="Microsoft.SharePoint">
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.SharePoint.Client.ServerRuntime">
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AuthenticationHelper.cs" />
Expand Down Expand Up @@ -165,6 +169,6 @@
</PropertyGroup>
<Import Project="$(VSToolsPath)\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup>
<PostBuildEvent>"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\x64\gacutil.exe" /f /i "$(TargetPath)"</PostBuildEvent>
<PostBuildEvent>"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64\gacutil.exe" /f /i "$(TargetPath)"</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion AzureCP/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("2.6")]
[assembly: AssemblyFileVersion("2.6.1")]

29 changes: 15 additions & 14 deletions AzureCP/TEMPLATE/ADMIN/AzureCP/AzureCPSettings.aspx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public partial class AzureCPSettings : LayoutsPageBase
string TextErrorNoTrustAssociation = "AzureCP is currently not associated with any TrustedLoginProvider. It is mandatory because it cannot create permission for a trust if it is not associated to it.<br/>Visit <a href=\"http://ldapcp.codeplex.com/\" target=\"_blank\">http://ldapcp.codeplex.com/</a> to see how to associate it.<br/>Settings on this page will not be available as long as AzureCP will not associated to a trut.";
string TextErrorAzureTenantFieldsMissing = "Some mandatory fields are missing.";
string TextErrorTestAzureADConnection = "Unable to connect to Azure tenant<br/>It may be expected if w3wp process of central admin has intentionally no access to Azure.<br/>{0}";
string TextErrorTestAzureADConnectionTenantNotFound = "Tenant was not found.";
string TextConnectionSuccessful = "Connection successful.";
string TextErrorNoIdentityClaimType = "The TrustedLoginProvider {0} is set with identity claim type \"{1}\" but it is not in the claims list of AzureCP.<br/>Please visit AzureCP page \"claims mapping\" in Security tab to set it and return to this page afterwards.";
string TextErrorPersistedObjectStale = "Modification is cancelled because persisted object was modified since last load of the page. Please refresh the page and try again.";
Expand All @@ -48,7 +49,7 @@ protected void Page_Load(object sender, EventArgs e)
return;
}

SPSecurity.RunWithElevatedPrivileges(delegate()
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
// Get SPPersisted Object and create it if it doesn't exist
PersistedObject = AzureCPConfig.GetFromConfigDB();
Expand Down Expand Up @@ -208,7 +209,7 @@ protected void UpdateTrustConfiguration()
void UpdatePersistedObject()
{
// Update object in database
SPSecurity.RunWithElevatedPrivileges(delegate()
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
this.Web.AllowUnsafeUpdates = true;
PersistedObject.Update();
Expand Down Expand Up @@ -238,22 +239,22 @@ protected void ValidateAzureTenantConnection()
string clientId = this.TxtClientId.Text;
string clientSecret = this.TxtClientSecret.Text;

// Get access token
activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication(tenantName, tenantId, clientId, clientSecret);

ITenantDetail tenant = null;
TenantDetail tenantDetail = null;
List<ITenantDetail> tenantsList = activeDirectoryClient.TenantDetails
// Get information on tenant
ITenantDetail tenant = activeDirectoryClient.TenantDetails
.Where(tDetail => tDetail.ObjectId.Equals(tenantId))
.ExecuteAsync().Result.CurrentPage.ToList();
if (tenantsList.Count > 0)
.ExecuteAsync()
.Result.CurrentPage.FirstOrDefault();
if (tenant != null)
{
tenant = tenantsList.First();
tenantDetail = (TenantDetail)tenant;
this.LabelTestTenantConnectionOK.Text = TextConnectionSuccessful;
this.LabelTestTenantConnectionOK.Text += "<br>" + tenant.DisplayName;
}
else
{
this.LabelErrorTestLdapConnection.Text = TextErrorTestAzureADConnectionTenantNotFound = "Tenant was not found.";
}

this.LabelTestTenantConnectionOK.Text = TextConnectionSuccessful;
this.LabelTestTenantConnectionOK.Text += "<br>" + tenantDetail.DisplayName;

activeDirectoryClient = null;
}
catch (AuthenticationException ex)
Expand Down

0 comments on commit 158f76c

Please sign in to comment.