我正在尝试使用我的一些模型(例如 CreatedAt、UpdatedAt、CreatedBy 和 UpdatedBy)来实现基本审计。
日期/时间部分已完成。INotifyPropertyChanging, INotifyPropertyChanged
当属性发生更改(实现)并且可以很好地更新对应的字段时,我会在我的模型上引发事件。
我只需要知道如何获取当前用户名,而不必每次实例化或获取模型的现有实例时都通过控制器。
我正在尝试使用我的一些模型(例如 CreatedAt、UpdatedAt、CreatedBy 和 UpdatedBy)来实现基本审计。
日期/时间部分已完成。INotifyPropertyChanging, INotifyPropertyChanged
当属性发生更改(实现)并且可以很好地更新对应的字段时,我会在我的模型上引发事件。
我只需要知道如何获取当前用户名,而不必每次实例化或获取模型的现有实例时都通过控制器。
引用 System.Web 并使用 HttpContext.Current.User.Identity.Name 就像一个魅力。
我不知道。谢谢。我这样做:
Membership.GetUser().UserName
哪种方式更快?
谈论身份的抽象方式是“主体” - 请参阅此线程。我希望这段代码可以让你在不知道登录实现的情况下获得身份:
public static class SomeUtilityClass {
public static string GetUsername() {
IPrincipal principal = Thread.CurrentPrincipal;
IIdentity identity = principal == null ? null : principal.Identity;
return identity == null ? null : identity.Name;
}
}
...
record.UpdatedBy = record.CreatedBy = SomeUtilityClass.GetUsername();
引用System.Web
和使用HttpContext.Current.User.Identity.Name
就像一种魅力。