简体   繁体   中英

How to store Active Directory user GUID using Entity Framework?

I have a site that authenticates using Active Directory. I am using the Entity Framework and I need to store references to this users. I don't want save AD users in the database. One way is to store the user guid as a String in an Entity.

class Entity
{
   String UserGUID ;
}

Is it possible to something like this:

class Entity
{
   UserPrincipal user;
}

Instead of passing the string GUID pass an object and make the Entity Framework treat the association somewhat as if it the UserPrincipal object was an entity. It doesn't have to be the class UserPrincipal, it could be a another class. I would like to deal with objects rather than strings. Querying Active directory is not a problem.

In summary, I would like to be able to associate an entity with a non-entity class by storing a String GUID in the database but loading it as an object.

[UPDATE]

Many classes might have multiple associations with the AD users and it can vary so a base class is not a solution. For example, I might have a class like this:

class Message
{
   public User Sender;
   public User Recipient;

   public List<User> MentionedUsers;
}

This is not a class I am using but it illustrates my point. Ideally the User guid would be stored in the Message entity table but be loaded as a User just like the Entity Framework does with other entites.

I am thinking creating User as a wrapper entity class to the GUID and retrieve properties with static methods but I would like to avoid this.

Seems easy enough for code first:

public class Entity {
    public string UserGUID { get; set; }

    [NotMapped]
    private UserPrincipal? _user;
    [NotMapped]
    public UserPrincipal User
    {
        get
        {
            if (!_user.HasValue)
                _user = UserPrincipal.GetUser(this.UserGUID);  // Make this static for easier re-use.

            return _user.Value;
        }
        set
        {
            UserGUID = value.UserGUID;
            _user = value;
        }
    }
}

[NotMapped] is your friend here (it's in System.ComponentModel.DataAnnotations ). You could simplify things by simply returning the function call every time you user get , eg: get { return this.GetUser(); } get { return this.GetUser(); } and remove the _user field, but that would impact performance.

I'm also not sure if you need [NotMapped] on a field (in this case _user ), I would try it with and without.

For a list of users:

public class Entity {
    public List<string> UserGUIDs { get; set; }

    [NotMapped]
    private List<UserPrincipal> _users;
    [NotMapped]
    public List<UserPrincipal> Users
    {
        get
        {
            if (_users != null)
                _users = UserPrincipal.GetUsers(this.UserGUIDs);

            return _users;
        }
        set
        {
            this.UserGUIDs = value.Select(u => u.UserGUID).ToList();
            _users = value;
        }
    }
}

Unfortunately there's not really a more elegant way to implement this with EF. Now, it wouldn't be all that difficult to alter EF to do this. EF is open source, fork it and get going if it's a big enough project to be worth it for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM