![](/img/trans.png)
[英]Azure Mobile Services .net Backend Authentication using individual user accounts
[英]Using Both Active Directory Authentication and Individual User Accounts in .NET Core
我的任務是創建一個可通過Active Directory或單個用戶帳戶進行身份驗證的.NET Core(C#MVC)應用程序。
關於建立一個或另一個,互聯網上有無數資源,我已經用它們創建了應用程序。 但是,兩者都有可能嗎?
OAuth似乎允許在.NET Core中開箱即用地進行多種身份驗證 ,但是我的猜測是Active Directory不太容易工作,需要在IIS中進行配置並使用操作系統進行授權。
如果無法同時進行這兩種操作,我有什么選擇? 我猜我會創建兩個單獨的項目,它們執行相同的操作,但是具有不同的身份驗證-但是維護兩個項目似乎不是一個好主意。
在此先感謝您的任何建議。
您可以按照自己的想法進行身份驗證,也可以將重點放在選擇IdentityServer4上,IdentityServer4是一個功能全面的身份驗證項目,可以滿足您的需求。
這是另一個與您要查找的內容有關的stackoverflow問題 。 如果您不了解IS4的工作原理,則可以在此處根據其模板創建項目。
dotnet new -i "identityserver4.templates::*"
我收集了一些資源,並決定創建一個簡單的自定義身份驗證,該身份驗證允許Active Directory和數據庫中的單個用戶帳戶。
首先,我將ASP.NET Identity添加到現有項目中 。 與鏈接的答案相比,我使Identity接口更加簡單:
IdentityConfig.cs
public class IdentityConfig
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() => new Entities());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Authentication/Login"),
});
}
}
按照@Sam關於在ASP.NET中創建自定義身份驗證/授權的回答(再次),然后我在數據庫中創建了一個簡單的Database-First用戶和角色表,而不是基於Identity,並創建了一個用戶管理器類:
UserManager.cs
public class UserManager
{
private Entities db = new Entities();
public bool IsValid(string username, string password)
{
// TODO: salt and hash.
return db.USER.Any(u => u.USERNAME == username && u.PASSWORD == password);
}
}
最后,為了完成自定義身份驗證,我創建了一個失效的簡單身份驗證控制器。 這將檢查用戶是否有效,然后創建ClaimIdentity
。
AuthenticationController.cs
public class AuthenticationController : Controller
{
private Entities db = new Entities();
public ActionResult Login()
{
return View();
}
public ActionResult Logout()
{
HttpContext.GetOwinContext().Authentication.SignOut();
return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult Login(string username, string password)
{
UserManager um = new UserManager();
bool valid = um.IsValid(username, password);
if (valid)
{
// get user role and enditem
USER user = db.USER.Where(u => u.USERNAME == username).First();
string role = db.ROLE.Where(r => r.USERID == user.USERID).FirstOrDefault().ROLENAME;
// create session
Claim usernameClaim = new Claim(ClaimTypes.Name, username);
Claim roleClaim = new Claim(ClaimTypes.Role, role);
ClaimsIdentity identity = new ClaimsIdentity(
new[] { usernameClaim, roleClaim }, DefaultAuthenticationTypes.ApplicationCookie
);
// auth succeed
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, identity);
return RedirectToAction("Index", "Home");
}
// invalid username or password
ViewBag.error = "Invalid Username";
return View();
}
}
這種簡單性似乎正是我想要的,而不是承擔Identity或ASP.NET成員身份的龐大任務,並且效果很好。
現在回答我的原始問題-如何同時容納Active Directory用戶?
在我提取並大大簡化了USER和ROLE類的同時,USER將需要大量額外的數據(角色,權限等)-並且該數據將不在Active Directory中-我們需要創建USER而不管。
因此,我只需要驗證Active Directory中的用戶名和密碼即可!
快速配置變量可以更改Login
操作中的控制流,並執行以下操作:
bool isValid = false;
if (authConfig == "AD") {
using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "US"))
{
// validate the credentials
isValid = pc.ValidateCredentials(username, password);
}
} else if (authConfig == "Custom") {
isValid = um.IsValid(username, password);
}
// create claim...
該解決方案之所以可行,是因為Active Directory身份驗證的唯一目的是驗證其用戶-不需要AD提供任何其他數據。 另外,由於我們需要在自定義表中指定角色和其他數據,因此無論如何都必須創建一個自定義用戶記錄。
我困惑的答案是缺乏對在.NET中創建自定義身份驗證而不使用其固有功能的靈活性的理解(例如,在創建新項目時檢查“個人用戶帳戶”選項)。
歡迎提出其他建議,因為這是我的ASP.NET知識的程度-但我認為這對我的應用程序有效。 我希望這有助於某人的身份驗證設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.