我還沒有玩過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;
希望這是沿着你正在尋找的線。
來源
2015-09-22 19:46:37
Ian
感謝您的回覆,我現在在4.5中做類似的事情,並在收到Windows身份驗證的初始身份後創建自定義身份。我沒有使用SSO。我今天可以用黑客入侵asp.net 5來嘗試獲得Windows身份驗證的工作,但想了解我是否應該朝着另一個方向前進,或者是否需要支持。 –
更新:我選擇了「無身份驗證」新項目對話框窗口,然後在調試選項卡下的項目屬性IIS Express設置我取消選中匿名身份驗證並檢查窗口身份驗證。之後,Controller類中提供的新用戶和Context.User屬性會自動填充AD中的聲明,這些聲明在讓IIS將用戶的域憑據傳遞給應用程序之前就已經習慣了。我假設我仍然可以攔截管道以在其上創建自定義聲明,但是我是否正確使用了AD?我有沒有使用AD的身份識別功能? –
我剛剛查看了GitHub的dotnet項目,並找不到System.DirectoryServices程序集,它告訴我它還不被支持。這個SO響應支持這個想法。 http://stackoverflow.com/a/28021962/755908。 因此,即使您爲Windows身份驗證設置了項目,也無法在.Net 5中使用DirectoryServices的自定義聲明。但是,一旦啓用,您就可以使用Active Directory實施SSO。 – Ian