示例#1
0
        private async Task DeleteUserAD(string objectId)
        {
            try
            {
                //Obtain B2C Settings
                var b2cDB = new ADB2CSettings()
                {
                    Id = 1
                };
                var b2cSettings = await _db.GetAsync(b2cDB);

                //Use Microsoft Graph to perform action on Azure AD B2C
                var client = new B2CGraphClient(
                    b2cSettings.AadClientId,
                    b2cSettings.AadClientSecret,
                    b2cSettings.AadTenant,
                    b2cSettings.AadGraphResourceId,
                    b2cSettings.AadGraphEndpoint,
                    b2cSettings.AadGraphVersion);
                await client.DeleteUser(objectId);
            }
            catch (Exception)
            {
                //Ignore if no AAD user is found
            }
        }
示例#2
0
        private async Task <ADUser> CreateUserAD(UserDto user)
        {
            //Obtain B2C Settings
            var b2cDB = new ADB2CSettings()
            {
                Id = 1
            };
            var b2cSettings = await _db.GetAsync(b2cDB);

            //Create a new user object
            var userObject = new JObject
            {
                { "accountEnabled", true },
                { "creationType", "LocalAccount" },
                { "displayName", user.FullName },
                { "passwordProfile", new JObject
                  {
                      { "password", "WSXzaq!23" },
                      { "forceChangePasswordNextLogin", true }
                  } },
                { "signInNames", new JArray
                  {
                      new JObject
                      {
                          { "type", "emailAddress" },
                          { "value", user.Email.Trim() }
                      }
                  } }
            };

            //Use Microsoft Graph to perform action on Azure AD B2C
            var client = new B2CGraphClient(
                b2cSettings.AadClientId,
                b2cSettings.AadClientSecret,
                b2cSettings.AadTenant,
                b2cSettings.AadGraphResourceId,
                b2cSettings.AadGraphEndpoint,
                b2cSettings.AadGraphVersion);
            var response = await client.CreateUser(userObject.ToString());

            var newUser = JsonConvert.DeserializeObject <ADUser>(response);

            return(newUser);
        }