2015-09-22 40 views
1

我用來創建內部asp.net Windows身份驗證應用程序,該應用程序可以自動填充WindowsPrincipal,該WindowsPrincipal擁有關於用戶名和一些我假設來自Active Directory的組信息的聲明。我安裝了Visual Studio 2015並嘗試使用ASP.NET 5 Web應用程序,但Windows身份驗證顯示爲灰色。我一直在學習基於聲明的身份驗證,並瞭解ASP.NET 5將帶來的跨平臺功能,並且我可能需要遠離Windows身份驗證。我應該在ASP.NET 5中使用Windows身份驗證嗎?

此內部Web應用程序位於Active Directory域中,但我不希望如果不需要請求用戶輸入憑據,因爲這將在IIS之上。如何應該我得到當前用戶?我是否需要選擇不同的身份驗證方法,並以某種方式通過其他方法從AD獲得聲明,或者Windows身份驗證仍然是此方案的推薦選項,並且最終將提供給您?

回答

1

我還沒有玩過v5,但是我在我的Intranet應用程序中使用了使用Windows身份的SSO。你有沒有試過GenericPrincipal?現在我很好奇,將需要與我的僱主談談.NET 5的測試......

使用GenericPrincipal和IIdentity,您可以從登錄到Windows的用戶的活動目錄中獲取任何內容。

這些都在我的Employee類中。

public Employee(Authenticate identity) 
{ 
    if (identity.IsAuthenticated) 
    { 
     // if the account exists, build the employee object 
     loadEmployee(identity as IIdentity); 
    } 
    else 
    { 
     // If the account cannot be found in Active Directory, throw an exception 
     throw new ArgumentOutOfRangeException(identity.Name, "Employee does not exist"); 
    } 
} 

private void loadEmployee(IIdentity identity) 
{ 
    // the domain name of the identity 
    string domain = identity.Name.Split('\\')[0]; 

    // the username of the identity 
    string username = identity.Name.Split('\\')[1]; 

    // Initializes a new context for the identity's domain 
    PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain); 

    // Initialize a principal using the domain context and the username of the identity 
    UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username); 

    // Build a collection of all the underlying objects 
    var userProperties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties; 

    // populate the employee objects properties 
    LastName = user.Surname; 
    FirstName = user.GivenName; 
    NtUserName = username; 
    GUID = user.Guid.ToString(); 
} 

在我的AuthenticationFilter類中,我構建了我的IPrincipal。

//build the system's Identity and Principal 
var genIdentity = new GenericIdentity(employee.FirstName + " " + employee.LastName); 
var genPrincipal = new GenericPrincipal(genIdentity, roles[]); 
var identity = (IPrincipal)genPrincipal; 
context.User = identity; 

希望這是沿着你正在尋找的線。

+0

感謝您的回覆,我現在在4.5中做類似的事情,並在收到Windows身份驗證的初始身份後創建自定義身份。我沒有使用SSO。我今天可以用黑客入侵asp.net 5來嘗試獲得Windows身份驗證的工作,但想了解我是否應該朝着另一個方向前進,或者是否需要支持。 –

+1

更新:我選擇了「無身份驗證」新項目對話框窗口,然後在調試選項卡下的項目屬性IIS Express設置我取消選中匿名身份驗證並檢查窗口身份驗證。之後,Controller類中提供的新用戶和Context.User屬性會自動填充AD中的聲明,這些聲明在讓IIS將用戶的域憑據傳遞給應用程序之前就已經習慣了。我假設我仍然可以攔截管道以在其上創建自定義聲明,但是我是否正確使用了AD?我有沒有使用AD的身份識別功能? –

+0

我剛剛查看了GitHub的dotnet項目,並找不到System.DirectoryServices程序集,它告訴我它還不被支持。這個SO響應支持這個想法。 http://stackoverflow.com/a/28021962/755908。 因此,即使您爲Windows身份驗證設置了項目,也無法在.Net 5中使用DirectoryServices的自定義聲明。但是,一旦啓用,您就可以使用Active Directory實施SSO。 – Ian

相關問題