0

Having trouble update users in AD

My Model:

public class UserModel
{
    ....
    [ScaffoldColumn(false)]
    [DisplayName("Fødselsdag")]
    [DataType(DataType.Date)]
    [NotMapped]
    public DateTime extensionAttribute1_date
    {
    get
    {
         try
         {
              return DateTime.Parse(extensionAttribute1);
         }
         catch (Exception e)
         {
              return new DateTime();
          }
      }
      set { }
    }
    }

My Controller:

[HttpPost]
        public ActionResult Edit(string sAMAccountName, FormCollection collection, UserModel data)
        {
            if (ModelState.IsValid)
            {
                var config = new LdapConfiguration();
                config.ConfigureFactory("domain.local").AuthenticateAs(new NetworkCredential("xxxx", "xxxxx"));
                using (var context = new DirectoryContext(config))
                {
                    var user = context.Query(new UserModel(), "OU=users,OU=xxx,DC=xxx,DC=dk", "User").FirstOrDefault(d => d.sAMAccountName == sAMAccountName);

                    if (user == null) return RedirectToAction("Index");

                    user.title = data.title;
                    user.mobile = data.mobile;
                    user.homePhone = data.homePhone;
                    user.streetAddress = data.streetAddress;
                    user.postalCode = data.postalCode;
                    user.l = data.l;
                    user.department = data.department;
                    user.physicalDeliveryOfficeName = data.physicalDeliveryOfficeName;
                    user.extensionAttribute1 = data.extensionAttribute1_date.ToLongDateString();

                    context.Update(user);
                }

                return RedirectToAction("Index");
            }

            return View();
        }

When i submit to Edit Action i results in an error: The requested attribute does not exist.

If i remove extensionAttribute1_date from the model i updates fine.

How do i exclude my calculated attributes from the update?

I have other attributes in the model such as Age which is calculated! Is this the wrong procedure for this?

/Michael

  • Have you tried removing the setter from the property? – Ben Robinson Oct 27 '14 at 09:40
  • tried removing the setter and the update went fine if i leave out extensionAttribute1_date. But i need the property in my View and when posted back assigned to another property to be saved. – Michael Falch Madsen Oct 27 '14 at 10:33
  • With the empty setter LINQ to LDAP doesn't have enough information to know that that property isn't updatable. If you need to leave the empty setter, you should look into attribute based mapping or an explicit class map for your object. – Alan Nov 07 '14 at 04:09

0 Answers0