c# - Galactic.ActiveDirectory 在 ASP MVC 中不起作用
问题描述
使用以下代码,我没有错误,但所有登录均因登录错误而失败。
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MVCFBAWithAD.Models;
using System.Security;
using System.Configuration;
using Galactic.ActiveDirectory;
using System.DirectoryServices.Protocols;
using System.Security.Principal;
我正在使用包Galactic.ActiveDirectory
。
这是控制器的一部分:
[Authorize]
public class AccountController : Controller
{
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
string serverName = ConfigurationManager.AppSettings["ADServer"];
if (ModelState.IsValid)
{
SecureString securePwd = null;
if (model.Password != null)
{
securePwd = new SecureString();
foreach (char chr in model.Password.ToCharArray())
{
securePwd.AppendChar(chr);
}
}
try
{
// Check user credentials
ActiveDirectory adVerifyUser = new ActiveDirectory(serverName, model.UserName, securePwd);
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToLocal(returnUrl);
}
catch
{
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
return View(model);
}
}
catch 块释放,变量有如下值。
ActiveDirectory adVerifyUser = new ActiveDirectory(serverName, model.UserName, securePwd)
我尝试了几种组合,但都不起作用。
带域名:
serverName = \\\\SCD1000
model.UserName = DOMAINNAME\\Dirk
securePWD = {System.Security.SecureString}
没有域名和反斜杠
serverName = SCD1000
model.UserName = Dirk
securePWD = {System.Security.SecureString}
.. 等等。
\\
您可以在字符串变量中看到双反斜杠。我认为这属于转义序列,这不是错误的原因吗?
任何人都可以帮助我或知道问题出在哪里吗?
解决方案
这不是服务器名称,而是域名
http://galacticapi.github.io/documentation/html/508a11b5-fa71-664c-10f0-15fab368078b.htm
ActiveDirectory 构造函数(字符串、字符串、SecureString、字符串)
domainName
Type: System.String
The DNS style domain name of the Active Directory to connect to.
userName
Type: System.String
The username of the account in AD to use when making the connection.
password
Type: System.Security.SecureString
The password of the account.
siteName (Optional)
推荐阅读
- android - 谷歌脚本:在一个项目中使用 MIT App Inventor 的多个 doPost
- go - 无法使用“go mod vendor”从 Kubernetes 集群连接到 Go 代理
- python - 通过过滤 sql 中的特定变量值从 cursor.execute(sql) 获取数据到 python 列表
- oracle - 如何创建批处理以将 Oracle 数据库数据上传到 AWS Data Exchange?
- python-3.x - 如何在 Pandas 中转换日期时间?
- mariadb - 错误!找不到 MariaDB 服务器 PID 文件
- google-cloud-platform - GCS 存储桶应该只能由特定人员访问
- c# - Azure 搜索 v11:索引复杂类型的可为空集合
- flutter - 如何在颤振中进行多页运行(应用程序)?
- spring - 如何在没有 Reactive 的情况下使用 Spring Cloud Gateway?