我有两个共享一个公共数据库的MVC应用程序。一个位于Internet服务器上,使用匿名身份验证和GateKeeper数据库,另一个位于Intranet服务器上,并使用Windows身份验证和Active Directory。
我想通过覆盖EntityFramework的SaveChanges方法来实现审计跟踪。在此覆盖中,我想记录登录用户的用户名。在外部站点上,它将是电子邮件地址,在Intranet站点上,它将是AD帐户名称。
应用程序都使用集成安全性连接到数据库,因此它们都运行在可以访问数据库的通用应用程序池帐户上。
我尝试过同时使用Environment.UserName
和System.Security.Principal.WindowsIdentity.GetCurrent()。名称,但两者都给了我一个应用程序池用户帐户,一个没有域名:
UserName = Environment.UserName //{ result = CAPETOWN\global-custom }
UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name //{ result = global-custom }
如何获得我想要的用户名
答案 0 :(得分:0)
你可以尝试这个
HttpContext.User.Identity.Name
答案 1 :(得分:0)
试试这个:
string UserName = HttpContext.Current.User.Identity.Name;
虽然如果您有特定代码的代码,它将不适合您的答案。
您可以获取所采取行动的屏幕网址 string _urlOfEntity = Url = HttpContext.Current.Request.CurrentExecutionFilePath;
为了覆盖SaveChanges()方法,您可以使用
审核代码编辑操作:
public int SaveChanges(object _olderInstance)
{
foreach (var ent in this.ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Modified || p.State == System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted))
{
// For each changed record, get the audit record entries and add them
//foreach (XmlTesst x in GetAuditRecordsForChange(ent))
//{
this.XmlTessts.Add(GetAuditRecordsForChange(ent));
//}
}
// Call the original SaveChanges(), which will save both the changes made and the audit records
return base.SaveChanges();
}
public XmlTesst GetAuditRecordsForChange(DbEntityEntry dbEntry)
{
string tableName, eventtype;
XmlTesst XmlForReturn;
if (dbEntry.State == System.Data.EntityState.Modified)
{
TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
// Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;
XmlDocument XmlCurrentDoc = new XmlDocument();
XmlNode rootNodeCurrent = XmlCurrentDoc.CreateElement(tableName);
XmlCurrentDoc.AppendChild(rootNodeCurrent);
XmlDocument XmlOldDoc = new XmlDocument();
XmlNode rootNodeOld = XmlOldDoc.CreateElement(tableName);
XmlOldDoc.AppendChild(rootNodeOld);
foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
{
object CurrentValues = dbEntry.CurrentValues.GetValue<object>(propertyName);
object OriginalValues = dbEntry.Entity.GetType().GetProperty(propertyName).GetValue(_olderInstancea, null);
if (!object.Equals(dbEntry.Entity.GetType().GetProperty(propertyName).GetValue(_olderInstancea, null), CurrentValues) && (OriginalValues != null || CurrentValues != null))
{
XmlNode userNodeCurrent = XmlCurrentDoc.CreateElement(propertyName);
userNodeCurrent.InnerText = CurrentValues.ToString();
rootNodeCurrent.AppendChild(userNodeCurrent);
XmlNode userNodeOld = XmlOldDoc.CreateElement(propertyName);
if (OriginalValues == null)
{
userNodeOld.InnerText = "Not Assigned ";
rootNodeOld.AppendChild(userNodeOld);
}
else
{
userNodeOld.InnerText = OriginalValues.ToString();
rootNodeOld.AppendChild(userNodeOld);
}
}
}
XmlForReturn = new XmlTesst() { OriginalXml = GetXMLAsString(XmlOldDoc), CurrentXml = GetXMLAsString(XmlCurrentDoc), EventType = "Edited", TimeofActivity = DateTime.Now, UserName = HttpContext.Current.User.Identity.Name, Url = HttpContext.Current.Request.CurrentExecutionFilePath, Screen = tableName };
return XmlForReturn;