3

I'm currently working in portal using ASP .NET Core. One of the requirements is to create Azure AD users, there are a couple of problems found on the way.

First, when trying to use the GraphClient SDK I get these compilation errors:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0012  The type 'IList<>' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.  PTIWebPortal.Packages.Cloud.DNX 4.6
D:\Eduardo\PTI Projects\PTIPortal\Portal\PTIPortal\PTIWebPortal.Packages.Cloud\CloudUserManager.cs  40  Active

That one happens when trying to set the OtherMails property of the object newUser.OtherMails = new System.Collections.Generic.List();

The other compilation error is

Severity    Code    Description Project File    Line    Suppression State
Error   CS0012  The type 'Uri' is defined in an assembly that is not referenced. 
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.  
PTIWebPortal.Packages.Cloud.DNX 4.6 
D:\Eduardo\PTI Projects\PTIPortal\Portal\PTIPortal\PTIWebPortal.Packages.Cloud\CloudUserManager.cs  43  Active

This one happens when trying to instantiate the ActiveDirectoryClient ActiveDirectoryClient adClient = new ActiveDirectoryClient(serviceRoot, null);

I think these two are errores due to the SDKs not yet fully compatible with .NET Core, since there is already a Uri type which I'm already using which is a different version

// Generated by .NET Reflector from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll

I was spending too much time on it so I decided to try using Microsoft Graph, but I keep getting a "Forbidden" response even after adding Read and Write Directory Data to the application in Azure AD this is the current code for that

public static readonly string CreateUserUrl = @"https://graph.microsoft.com/{0}/users";
public static async Task<UserInfo> CreateUser(string accessToken, UserInfo pUser)
        {
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, Settings.CreateUserUrl.v10Version()))
                {
                    request.Headers.Accept.Add(Json);
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    var userData = new
                    {
                        accountEnabled = true,
                        displayName = pUser.DisplayName,
                        mailNickname = pUser.Username,
                        passwordProfile = new
                        {
                            password = pUser.Password,
                            forceChangePasswordNextSignIn = false
                        },
                        userPrincipalName = string.Format("{0}@{1}", pUser.Username, pUser.Domain)
                    };
                    string serializedData = JsonConvert.SerializeObject(userData);
                    request.Content = new StringContent(serializedData, System.Text.Encoding.UTF8, "application/json");
                    //https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations
                    //http://stackoverflow.com/questions/35845541/microsoft-graph-rest-api-add-attachment-to-email-using-c-sharp-asp-net-mvc

                    using (var response = await client.SendAsync(request))
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            var json = JObject.Parse(await response.Content.ReadAsStringAsync());
                            //myInfo.DisplayName = json?["displayName"]?.ToString();
                            //myInfo.MailAddress = json?["mail"]?.ToString().Trim().Replace(" ", string.Empty);
                            //myInfo.Department = json?["department"]?.ToString();
                            //myInfo.PhotoBytes = await GetUserPhotoAsync(accessToken, json?["userPrincipalName"]?.ToString());
                        }
                    }
                }
            }
            return pUser;
        }

Note: I am already able to log in as an Azure AD User, and I'm also able to get information using Microsoft Graph.

Any ideas what I could do to resolve either of the two problems?

  • Create Azure AD Users using .NET SDKs from within a .NET Core app
  • Resolve the "Forbidden" problem trying to create users using Microsoft Graph

3 Answers 3

3

If you add this to project.json under dependencies it should fix the compatibility issue

"Microsoft.NETCore.Portable.Compatibility": "1.0.1"
Sign up to request clarification or add additional context in comments.

Comments

1

To fix the compilation error for "The type 'IList<>' is defined in an assembly that is not referenced. You must add a reference to assembly" you should add the following into web.config:

<assemblies>
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>

This will resolve the first error.

Regarding the second error, try to make sure to set the right permissions in your Azure AD registration so you have read and write permissions.

Hope this helps.

1 Comment

Thanks, it helped, with a small modification since I'm using project.json
1

Found the solution to the first problem using Azure SDK, had to add the dependenci, but in the Famework Assemblies section in project.json

"frameworks": {
    "dnx46": {
      "dependencies": {
        "Microsoft.Azure.ActiveDirectory.GraphClient": "2.1.0",
        "Microsoft.Azure.Common": "2.1.0",
        "Microsoft.Azure.Management.Resources": "3.4.0-preview",
        "Microsoft.Azure.Management.Websites": "1.1.0-preview",
        "Microsoft.Azure.Gallery": "2.6.2-preview",
        "Microsoft.Azure.Common.Dependencies": "1.0.0",
        "Microsoft.WindowsAzure.Common": "1.4.1",
        "Microsoft.WindowsAzure.Management.MediaServices": "4.1.0",
        "Microsoft.WindowsAzure.Management.Storage": "5.1.1",
        "Microsoft.WindowsAzure.Management.Compute": "12.3.1",
        "Microsoft.WindowsAzure.Management.Libraries": "2.0.0",
        "WindowsAzure.MediaServices": "3.5.2",
        "windowsazure.mediaservices.extensions": "3.3.0",
        "Microsoft.IdentityModel.Clients.ActiveDirectory": "3.9.302261508-alpha",
        "Microsoft.Framework.WebEncoders": "1.0.0-beta8",
      },
      "frameworkAssemblies": {
        "System.Runtime": "4.0.20.0",
        "System.Threading.Tasks": "4.0.10.0"
      }
    }
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.