问题描述
我正在创建一个项目,其中需要在 MVC 应用程序中实现 Windows Active Directory 登录系统。我已经从谷歌尝试了很多东西,但我正在努力寻找一种方法。
其实我想从头开始了解这个过程。
有没有人有任何见解或链接到一个很好的解释? Google 没有提供太多帮助。
<httpCookies httpOnlyCookies="true" />
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/User/LoginUser" timeout="45"
slidingExpiration="true" protection="All" />
</authentication>
<membership defaultProvider="ADMembershipProvider">
<providers>
<clear />
<add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider"
connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
<connectionStrings>
<add name="ADConnectionString" connectionString="LDAP://domain:389/DC=domain,DC=Local" />
<connectionStrings>
我的控制器在下面。这里有一个错误显示 LDAP 连接字符串不代表有效的 LDAP adspath。
public ActionResult LoginUser()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult LoginUser(LoginUser login,string returnUrl)
{
if (ModelState.IsValid)
{
MembershipProvider domainProvider = Membership.Providers["ADMembershipProvider"];
if (domainProvider.ValidateUser(login.UserName,login.Password))
{
FormsAuthentication.SetAuthCookie(login.UserName,true);
if(Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index","Service");
}
}
}
else
{
ModelState.AddModelError("","The user name or password provided is incorrect");
}
return View(login);
}
这是我的登录视图:
<div class="row mt-5 ">
<div class="col-sm-6 offset-sm-3 bodyColor ">
<div class="col-sm-8 offset-sm-2">
<div style="max-width: 400px;">
<br />
<h2>
Login
</h2>
<br />
@Html.LabelFor(model => model.UserName)
@Html.TextBoxFor(model => model.UserName,new { @class = "form-control",style = "width:325px;" })
@Html.ValidationMessageFor(model => model.UserName,"",new { @class = "textRed" })
<br />
@Html.LabelFor(model => model.Password)
@Html.TextBoxFor(model => model.Password,style = "width:325px;",type = "password" })
@Html.ValidationMessageFor(model => model.Password,new { @class = "textRed" })
<br />
@Html.CheckBoxFor(model => model.RememberMe)
@Html.LabelFor(model => model.RememberMe)
<input type="hidden" name="ReturnUrl" value="@Request.QueryString["ReturnUrl"]" />
<div class="col-sm-8 offset-sm-2">
<input type="submit" value="Login" class="btn btn-default" style="width:90px;" />
<input type="submit" value="Cancel" class="btn btn-default" style="width:90px;" />
</div>
</div>
</div>
<br />
<br />
<div id="dvMessage" style="display:none" class="alert alert-danger">
<strong>Error!</strong>
<span>@ViewBag.Message</span>
</div>
</div>
<br />
<br />
</div>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)