题
我在努力执行基本审计与我的一些模型(如代码运行时出现的错误,UpdatedAt,创建和UpdatedBy).
日期/时间的一部分。我扔的事件在我的模型时的财产被改变(执行 INotifyPropertyChanging, INotifyPropertyChanged
)和可更新的通讯领域就好了。
我只需要知道如何把当前用户名称没有通过它通过控制器,每次我化或获得的现有实例中的一种模型。
解决方案
参考系统。网和使用 HttpContext.电流。用户。身份。名称 工作就像一个特色。
我不知道。谢谢。我做这样的:
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
工作就像一个特色。
不隶属于 StackOverflow