[英]How I can find a User with the GUID(objectGUID) Parameter in Active Directory
[英]How to store Active Directory user GUID using Entity Framework?
我有一个使用Active Directory进行身份验证的站点。 我正在使用实体框架,并且需要存储对此用户的引用。 我不想将AD用户保存在数据库中。 一种方法是将用户guid作为字符串存储在实体中。
class Entity
{
String UserGUID ;
}
是否可能是这样的:
class Entity
{
UserPrincipal user;
}
不用传递字符串GUID而是传递对象并使实体框架将关联视为UserPrincipal对象是一个实体。 它不必是UserPrincipal类,也可以是另一个类。 我想处理对象而不是字符串。 查询活动目录不是问题。
总而言之,我希望能够通过在数据库中存储String GUID并将其作为对象加载,从而将实体与非实体类相关联。
[更新]
许多类可能与AD用户具有多个关联,并且可能有所不同,因此基类不是解决方案。 例如,我可能有一个这样的类:
class Message
{
public User Sender;
public User Recipient;
public List<User> MentionedUsers;
}
这不是我正在使用的课程,但它说明了我的观点。 理想情况下,用户guid将存储在消息实体表中,但是像实体框架与其他实体一样作为用户加载。
我正在考虑将User创建为GUID的包装实体类,并使用静态方法检索属性,但我想避免这种情况。
看起来很容易进行代码优先:
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]
是您的朋友(在System.ComponentModel.DataAnnotations
)。 您可以通过在每次用户get
时简单地返回函数调用来简化操作,例如: get { return this.GetUser(); }
get { return this.GetUser(); }
并删除_user
字段,但这会影响性能。
我也不确定您是否需要在字段上使用[NotMapped]
(在本例中为_user
),无论是否使用,我都可以尝试。
对于用户列表:
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;
}
}
}
不幸的是,实际上并没有更优雅的方法可以用EF来实现。 现在,改变EF做到这一点并不难。 EF是开源的,如果它是一个足够大的项目,对您来说值得的话,请继续努力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.