更新时间:2023-12-03 09:37:52
我这样解决了设置视图模型和控制器的问题:
模型
公共类LogOnModel
{
[必需]
[Display(Name = User name))]
公共字符串UserName {get;组; }
[必需]
[DataType(DataType.Password)]
[Display(Name = Password)]
公共字符串Password {get;组; }
[Display(Name = Remember me?)]
public bool RememberMe {get;组; }
}
控制器
公共类AccountController:控制器
{
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(,提供的用户名或密码不正确);
}
}
//如果到此为止,则出现故障,重新显示
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>登录< / button>
< / div>
}
web.config与我的问题相同
I want to authenticate users in my asp.net mvc project using active directory, after hours and hours spent surfing on the internet i didn't find anything useful for me, I've already saw all the result but nothing.
I tryed to edit my web.config as many post suggests.
If anyone can help me with pieces of code or example i'll appreciate it a lot, because i have no idea where i can start from.
EDIT
My current 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>
Leo
I solved my problem setting view model and controller like this:
Model
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");
}
View
<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>
}
And the web.config remain the same as my question