嘿伙計們只是一個沒有代碼的直接問題。我正在努力尋找簡單問題的基本答案。
我到底應該如何使用 Windows 身份驗證(不是集成的 Windows 身份驗證)和 Active Directory 進行身份驗證然后授權?你會認為會有一百萬個示例專案,但我對 .net core 的搜索結果中有 80% 都出現了舊的 asp.net 文章,這些文章已經過時并且不適用于新版本。
我在這里有一堆作業示例,但它們都使用 System.Web 類/庫較舊,這些類/庫現在都已棄用,不再對我有用。
我從舊的 asp.net 專案中獲得的代碼和方法看起來都很簡單,否則我很困惑為什么我找不到任何類似的 .net 核心?在我閱讀的大多數文章中,一切都是一些利基第三方包,而我只是想用普通的微軟方式來做,我發現很難相信沒有一個簡單的解決方案可以用于登錄螢屏和針對 AD 的身份驗證。Microsoft 檔案感覺他們的目標是獲得小提示并確切知道該做什么的專家。
我是一名剛作業了 4 個月的新研究生,并且是 .net 核心的新手。
我已經了解了 LDAP、宣告、主體、cookie 和更多的兔子洞,但對于所有不同版本的 .net 及其類/庫等,我感到比任何事情都更加困惑。
uj5u.com熱心網友回復:
有一個 nuget 可以處理這個問題;System.DirectoryServices.AccountManagement
它只是 Windows,有一個我認為是跨平臺的 Novel ldap 版本。
要進行身份驗證:
using (var ctx = new PrincipalContext(ContextType.Domain))
{
if (!ctx.ValidateCredentials(user_name, password))
throw new Exception("unknown username or password");
using (var userPrinciple = new UserPrincipal(ctx)) {
userPrinciple.SamAccountName = user_name;
using (var search = new PrincipalSearcher(userPrinciple))
{
UserPrincipal user = (UserPrincipal) search.FindOne();
if (user == null) {
throw new Exception("user authenticated but not found in directory");
}
return user; // auth'ed user
}
}
}
授權(按組成員身份):
using (var ctx = new PrincipalContext(ContextType.Domain))
{
using (var groupPrinciple = new GroupPrincipal(ctx))
{
groupPrinciple.SamAccountName = groupName;
using (var search = new PrincipalSearcher(groupPrinciple))
{
member_list = GetMembersOfPrincipalGroup((GroupPrincipal)search.FindOne());
}
// member_list contains all the users of a group.
// I cache these in a Dictionary for faster group membership checks
}
}
請注意,ContextType 列舉處理本地計算機用戶以及域。在 nuget 包中搜索更多示例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416838.html
標籤:
