AD域账户登录mysql_ASP.NET Core AD 域登录
在選擇AD登錄時(shí),其實(shí)可以直接選擇 Windows 授權(quán),不過(guò)因?yàn)橛行┚W(wǎng)站需要的是LDAP獲取信息進(jìn)行授權(quán),而非直接依賴(lài)Web Server自帶的Windows 授權(quán)功能。 當(dāng)然如果使用的是Azure AD/企業(yè)賬號(hào)登錄時(shí),直接在ASP.NET Core創(chuàng)建項(xiàng)目時(shí)選擇就好了。
來(lái)個(gè)ABC:
1.新建一個(gè)ASP.NET Core項(xiàng)目
2.Nuget引用dependencies / 修改```project.json```
Novell.Directory.Ldap.NETStandard
Microsoft.AspNetCore.Authentication.Cookies
版本如下:
"Novell.Directory.Ldap.NETStandard": "2.3.5",
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0" 本文的AD登錄使用的是第三方的
```Novell.Directory.Ldap.NETStandard``` 進(jìn)行的LDAP操作(還沒(méi)有看這個(gè)LDAP的庫(kù)是否有安全性問(wèn)題,如果有需要修改或更換)
3.建立一個(gè)LDAP操作的工具類(lèi)
代碼在下面鏈接中,就不單獨(dú)貼了,基本上就2個(gè)方法:
Register是獲取基本配置信息的
Validate是來(lái)驗(yàn)證用戶(hù)名密碼的
https://github.com/chsword/aspnet-core-ad-authentication/blob/master/src/Demo/LDAPUtil.cs
4.在applicationSettings.json中添加基本的域配置
"LDAPServer": "192.168.1.1",//域服務(wù)器
"LDAPPort": 389,//端口,一般默認(rèn)就是這個(gè)
"CookieName": "testcookiename",//使用Cookie登錄的Cookie的Key
"BindDN": "CN=DoWebUser,CN=Users",//用來(lái)獲取LDAP的信息用戶(hù)的用戶(hù)名
"BindPassword": "!DoWebUserPassword",//用來(lái)獲取LDAP的信息的用戶(hù)的密碼,即DoWebUser的密碼
"LDAPBaseDC": "DC=aspnet,DC=com",//域的DC
5.Startup.cs中修改
Startup方法中:
LDAPUtil.Register(Configuration); ConfigureServices 方法中:
services.AddAuthorization(options =>{}); Configure方法中:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = Configuration.GetValue("CookieName"),
LoginPath = new PathString("/Account/Login/"),
AccessDeniedPath = new PathString("/Account/Login/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
}); 6.AccountController中添加登錄和注銷(xiāo)的Action
登錄的頁(yè)面:
[AllowAnonymous]
public IActionResult Login()
{
return View();
} 登錄的Post頁(yè)面:
[HttpPost]
[AllowAnonymous]
public async Task Login(string u, string p)
{
if (LDAPUtil.Validate(u, p))
{
var identity = new ClaimsIdentity(new MyIdentity(u));//這個(gè)MyIdentity只是一個(gè)祼的IIdentity的實(shí)現(xiàn)的類(lèi)
var principal = new ClaimsPrincipal(identity);
await HttpContext.Authentication.SignInAsync(LDAPUtil.CookieName, principal);
return RedirectToAction("Index", "Home");
}
return View();
} 注銷(xiāo)的頁(yè)面:
[Authorize]
public async Task Logout()
{
await HttpContext.Authentication.SignOutAsync(LDAPUtil.CookieName);
return RedirectToAction("Index", "Home");
} Demo
https://github.com/chsword/aspnet-core-ad-authentication
引用
https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard
https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Cookies/
總結(jié)
以上是生活随笔為你收集整理的AD域账户登录mysql_ASP.NET Core AD 域登录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c++ 一个函数包括多个返回值判断_Py
- 下一篇: mysql对表中添加属性_菜鸟笔记—数据