[英]Should I Use Windows Authentication in ASP.NET 5?
我習慣於創建內部的asp.net Windows身份驗證應用程序,該應用程序自動填充WindowsPrincipal,該WindowsPrincipal聲明了有關用戶名和某些組信息的聲明,我認為這些信息來自Active Directory。 我安裝了Visual Studio 2015,並嘗試使用ASP.NET 5 Web應用程序,但Windows身份驗證顯示為灰色。 我一直在學習基於聲明的身份驗證,並且還了解ASP.NET 5將帶來的跨平台功能,並且我可能需要擺脫Windows身份驗證。
但是,此內部Web應用程序位於Active Directory域中,如果不需要,我希望不必詢問用戶憑據,因為它將放在IIS之上。 我應該如何獲得當前用戶? 我是否需要選擇其他身份驗證方法,並以某種方式通過另一種方法從AD索賠,還是Windows身份驗證仍然是此方案的推薦選項,並且最終將可用?
我還沒有玩過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;
希望這是您所尋找的路線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.