簡體   English   中英

使用Windows身份驗證和匿名身份驗證獲取用戶名

[英]Get username with Windows Authentication and anonymous authentication

我有2個共享公用數據庫的MVC應用程序。 一個位於Internet服務器上,對GateKeeper數據庫使用匿名身份驗證,另一個位於Intranet服務器上,對Active Directory使用Windows身份驗證。

我想通過重寫EntityFramework的SaveChanges方法來實現審核跟蹤。 在此替代中,我要記錄已登錄用戶的用戶名。 在外部站點上,它將是電子郵件地址,在內聯網站點上,它將是AD帳戶名。

這兩個應用程序都使用集成的安全性來連接到數據庫,因此它們都在可以訪問數據庫的通用應用程序池帳戶上運行。

我已經嘗試使用Environment.UserName和System.Security.Principal.WindowsIdentity.GetCurrent()。Name,但是都給了我應用程序池用戶帳戶,一個沒有域:

UserName = Environment.UserName //{ result = CAPETOWN\global-custom }
UserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name //{ result = global-custom }

如何獲得所需的用戶名

好吧,你可以試試這個

HttpContext.User.Identity.Name

嘗試這個:

string UserName = HttpContext.Current.User.Identity.Name;

盡管如果您以特定的方式編寫代碼,這將不適合設計,請為您的答案添加更多細節。

您可以獲得在其上執行操作的屏幕的URL字符串_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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM