日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

AD域账户登录mysql_ASP.NET Core AD 域登录

發布時間:2023/12/20 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AD域账户登录mysql_ASP.NET Core AD 域登录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在選擇AD登錄時,其實可以直接選擇 Windows 授權,不過因為有些網站需要的是LDAP獲取信息進行授權,而非直接依賴Web Server自帶的Windows 授權功能?! ‘斎蝗绻褂玫氖茿zure AD/企業賬號登錄時,直接在ASP.NET Core創建項目時選擇就好了。

來個ABC:

1.新建一個ASP.NET Core項目

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``` 進行的LDAP操作(還沒有看這個LDAP的庫是否有安全性問題,如果有需要修改或更換)

3.建立一個LDAP操作的工具類

代碼在下面鏈接中,就不單獨貼了,基本上就2個方法:

Register是獲取基本配置信息的

Validate是來驗證用戶名密碼的

https://github.com/chsword/aspnet-core-ad-authentication/blob/master/src/Demo/LDAPUtil.cs

4.在applicationSettings.json中添加基本的域配置

"LDAPServer": "192.168.1.1",//域服務器

"LDAPPort": 389,//端口,一般默認就是這個

"CookieName": "testcookiename",//使用Cookie登錄的Cookie的Key

"BindDN": "CN=DoWebUser,CN=Users",//用來獲取LDAP的信息用戶的用戶名

"BindPassword": "!DoWebUserPassword",//用來獲取LDAP的信息的用戶的密碼,即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中添加登錄和注銷的Action

登錄的頁面:

[AllowAnonymous]

public IActionResult Login()

{

return View();

}  登錄的Post頁面:

[HttpPost]

[AllowAnonymous]

public async Task Login(string u, string p)

{

if (LDAPUtil.Validate(u, p))

{

var identity = new ClaimsIdentity(new MyIdentity(u));//這個MyIdentity只是一個祼的IIdentity的實現的類

var principal = new ClaimsPrincipal(identity);

await HttpContext.Authentication.SignInAsync(LDAPUtil.CookieName, principal);

return RedirectToAction("Index", "Home");

}

return View();

}  注銷的頁面:

[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/

總結

以上是生活随笔為你收集整理的AD域账户登录mysql_ASP.NET Core AD 域登录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。