如何解决如何在 ASP.NET MVC 中使用 Windows Active Directory 登录?
我正在创建一个项目,其中需要在 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 举报,一经查实,本站将立刻删除。