Created
June 14, 2016 17:54
-
-
Save KaiWalter/69ec5ee7db22f55f5049e1fb83280ef1 to your computer and use it in GitHub Desktop.
Azure B2C replace DisplayName=Unkown with FirstName+LastName
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Configuration; | |
using Microsoft.Azure.ActiveDirectory.GraphClient; | |
using System.Reflection; | |
/// <summary> | |
/// http://www.eliostruyf.com/building-daemon-or-service-app-with-the-microsoft-graph-api/ | |
/// https://github.com/Azure-Samples/active-directory-dotnet-daemon-certificate-credential | |
/// https://azure.microsoft.com/en-us/blog/enabling-command-line-or-continuous-delivery-of-azure-webjobs/ | |
/// </summary> | |
namespace DirectoryCleaner | |
{ | |
class Program | |
{ | |
private static string Thumbprint = ConfigurationManager.AppSettings["Thumbprint"]; | |
private static string GraphUrl = ConfigurationManager.AppSettings["GraphUrl"]; | |
private static string TenantId = ConfigurationManager.AppSettings["TenantId"]; | |
private static string Authority = ConfigurationManager.AppSettings["Authority"]; | |
private static string ClientId = ConfigurationManager.AppSettings["ClientId"]; | |
private static string GraphTenantUrl = GraphUrl + TenantId; | |
private static string Mode = ConfigurationManager.AppSettings["Mode"].ToUpper(); | |
private static X509Certificate2 _cert; | |
private static ActiveDirectoryClient _client; | |
static void Main() | |
{ | |
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); | |
Console.WriteLine("Version : {0}", version); | |
Console.WriteLine("--------------------"); | |
Console.WriteLine("Authority : {0}", Authority); | |
Console.WriteLine("TenantId : {0}", TenantId); | |
Console.WriteLine("ClientId : {0}", ClientId); | |
Console.WriteLine("GraphTenantUrl : {0}", GraphTenantUrl); | |
Console.WriteLine("--------------------"); | |
Console.WriteLine("Mode : {0}", Mode); | |
MainAsync().Wait(); | |
#if DEBUG | |
Console.ReadKey(); | |
#endif | |
} | |
static async Task MainAsync() | |
{ | |
_cert = GetCertificate(); | |
var graphUri = new Uri(GraphTenantUrl); | |
_client = new ActiveDirectoryClient(graphUri, async () => await GetAccessToken()); | |
Console.WriteLine("--------------------"); | |
await CleanDisplayNameUnknown(); | |
Console.WriteLine("--------------------"); | |
} | |
static async Task CleanDisplayNameUnknown() | |
{ | |
Console.WriteLine("cleanup DisplayName=unknown"); | |
List<IUser> users = new List<IUser>(); | |
try | |
{ | |
var pageCollection = await _client.Users. | |
Where(user => user.DisplayName == "unknown"). | |
Take(100).ExecuteAsync(); | |
List<IUser> usersPage = pageCollection.CurrentPage.ToList(); | |
users.AddRange(usersPage); | |
while (pageCollection.MorePagesAvailable) | |
{ | |
pageCollection = await pageCollection.GetNextPageAsync(); | |
usersPage = pageCollection.CurrentPage.ToList(); | |
users.AddRange(usersPage); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
int iUnknownFound = 0; | |
foreach (var u in users) | |
{ | |
iUnknownFound++; | |
string displayName = u.GivenName + " " + u.Surname; | |
Console.WriteLine("{0}|{1}->{2}|{3} {4}", u.UserPrincipalName, u.DisplayName, displayName, u.GivenName, u.Surname); | |
if (Mode.Equals("CORRECT")) | |
{ | |
u.DisplayName = displayName; | |
await u.UpdateAsync(); | |
} | |
} | |
Console.WriteLine("Entries with unknown DisplayName : {0}", iUnknownFound); | |
} | |
private static X509Certificate2 GetCertificate() | |
{ | |
X509Certificate2 certificate = null; | |
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); | |
certStore.Open(OpenFlags.ReadOnly); | |
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, Thumbprint, false); | |
// Get the first cert with the thumbprint | |
if (certCollection.Count > 0) | |
{ | |
certificate = certCollection[0]; | |
} | |
certStore.Close(); | |
return certificate; | |
} | |
private static async Task<string> GetAccessToken() | |
{ | |
var authenticationContext = new AuthenticationContext(Authority, false); | |
var cac = new ClientAssertionCertificate(ClientId, _cert); | |
var authenticationResult = await authenticationContext.AcquireTokenAsync(GraphUrl, cac); | |
return authenticationResult.AccessToken; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment