0

我正在尝试使用我的一些模型(例如 CreatedAt、UpdatedAt、CreatedBy 和 UpdatedBy)来实现基本审计。

日期/时间部分已完成。INotifyPropertyChanging, INotifyPropertyChanged当属性发生更改(实现)并且可以很好地更新对应的字段时,我会在我的模型上引发事件。

我只需要知道如何获取当前用户名,而不必每次实例化或获取模型的现有实例时都通过控制器。

4

3 回答 3

2

引用 System.Web 并使用 HttpContext.Current.User.Identity.Name 就像一个魅力。

我不知道。谢谢。我这样做:

Membership.GetUser().UserName

哪种方式更快?

于 2009-05-04T18:10:35.723 回答
2

谈论身份的抽象方式是“主体” - 请参阅此线程。我希望这段代码可以让你在不知道登录实现的情况下获得身份:

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();
于 2009-05-04T19:38:08.553 回答
1

引用System.Web和使用HttpContext.Current.User.Identity.Name就像一种魅力。

于 2009-05-04T17:38:08.360 回答