c# – 在DbContext中访问HttpContext.Current.User.Identity.Name
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 在DbContext中访问HttpContext.Current.User.Identity.Name,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2604字,纯文字阅读大概需要4分钟。
内容图文
我将使用UserName跟踪Created和Modified字段.为此,我直接在DbContext中引用了System.Web程序集:
public void auditFields()
{
var auditDate = DateTime.Now;
foreach (var entry in this.ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
case EntityState.Added:
entry.Entity.CreatedOn = auditDate;
entry.Entity.ModifiedOn = auditDate;
entry.Entity.CreatedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
break;
case EntityState.Deleted:
break;
case EntityState.Modified:
entry.Entity.ModifiedOn = auditDate;
entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
它可以工作,但是它将DbContext与HttpContext紧密耦合,如果我们将DbContext暴露给非Web环境,这不是一个好主意.
所以我用这种方式:
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>,
IUnitOfWork
{
public ApplicationDbContext()
: base("ConnectionString")
{
}
public ApplicationDbContext(string userName)
: base("ConnectionString")
{
UserName = userName;
}
//Other codes
public string UserName
{
get;
private set;
}
public void auditFields()
{
var auditDate = DateTime.Now;
foreach (var entry in this.ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
case EntityState.Added:
entry.Entity.CreatedOn = auditDate;
entry.Entity.ModifiedOn = auditDate;
entry.Entity.CreatedBy = UserName ?? "anonymouse";
entry.Entity.ModifiedBy = UserName ?? "anonymouse";
break;
case EntityState.Deleted:
break;
case EntityState.Modified:
entry.Entity.ModifiedOn = auditDate;
entry.Entity.ModifiedBy = UserName ?? "anonymouse";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
在Ioc配置项目中(我在另一个类库中使用了structureMap):
ioc.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationDbContext>()
.Ctor<string>().Is(HttpContext.Current.User.Identity.Name);
但是当我运行应用程序时,我会在上面的行中收到此错误:
Object reference not set to an instance of an object
看来它无法注入HttpContext.
任何的想法?
解决方法:
看一下这个链接http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing
作者的解决方案看起来像你的(但他使用的是AutoFac而不是StructureMap).获得“名字”的“技巧”是Thread.CurrentPrincipal.Identity.Name;
另一件事,恕我直言,我认为你应该使用DateTimeOffSet而不是DateTime作为审计日期.使用DateTimeOffSet,您不会遇到不同的时区问题.像这样:
DateTimeOffSet auditDate = DateTime.UtcNow;
内容总结
以上是互联网集市为您收集整理的c# – 在DbContext中访问HttpContext.Current.User.Identity.Name全部内容,希望文章能够帮你解决c# – 在DbContext中访问HttpContext.Current.User.Identity.Name所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。