gpt4 book ai didi

c# - 在 asp.net MVC 中使用事件目录进行身份验证

转载 作者:行者123 更新时间:2023-12-03 22:10:35 25 4
gpt4 key购买 nike

我想使用事件目录对我的 asp.net mvc 项目中的用户进行身份验证,经过数小时和数小时在互联网上冲浪后,我没有找到任何对我有用的东西,我已经看到了所有结果,但什么也没看到。

我尝试按照许多帖子的建议编辑我的 web.config。

如果有人可以帮助我提供代码或示例,我将不胜感激,因为我不知道从哪里开始。

编辑

我当前的 web.config

<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/MainMenu/Login" timeout="45"
slidingExpiration="false" protection="All" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
<membership defaultProvider="ADMembershipProvider">
<providers>
<clear />
<add name="ADMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider"
connectionStringName="ADConnectionString"
attributeMapUsername="sAMAccountName" />
</providers>
</membership>
</system.web>
<connectionStrings>
<add name="ADConnectionString"
connectioString="LDAP://myserver.mydomain.COM:389/DC=mydomain,DC=COM" />
</connectionStrings>

狮子座

最佳答案

我解决了设置 View 模型和 Controller 的问题,如下所示:

模型

public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}

Controller
public class AccountController : Controller
{
public ActionResult LogOn()
{
return View();
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Main", "MainMenu");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect");
}
}

// if we got this far, something failed, redisplay form
return View(model);
}

public ActionResult LogOff()
{
FormsAuthentication.SignOut();

return RedirectToAction("Login", "Account");
}

看法
<body id="bodyMain">
@using (Html.BeginForm("LogOn", null,FormMethod.Post))
{
<div class="container">
@Html.ValidationSummary(true, "")
<div style="padding-top:30%"></div>
<table>
<tr>
<td>@Html.EditorFor(model => model.UserName)</td>
<td>@Html.ValidationMessageFor(model => model.UserName, "",)</td>
</tr>
<tr></tr>
<tr>
<td>@Html.EditorFor(model => model.Password)</td>
<td>@Html.ValidationMessageFor(model => model.Password, "")</td>
</tr>
<tr>
<td>@Html.CheckBoxFor(model=>model.RememberMe)</td>
</tr>
</table>
<br />
<button type="submit" class="btn btn-info">Login</button>
</div>
}

并且 web.config 与我的问题相同

关于c# - 在 asp.net MVC 中使用事件目录进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54646212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com