使用ASP.NET MVC中的活动目录进行身份验证 [英] Authentication using active directory in asp.net MVC
问题描述
我想使用活动目录在asp.net mvc项目中对用户进行身份验证,花了数小时在Internet上冲浪之后,我没有发现对我有用的东西,但我已经看到了所有结果,但一无所获。 / p>
我试图按照许多帖子的建议编辑web.config。
如果有人可以帮助我,代码或示例,我将不胜感激,因为我不知道从哪里开始。
EDIT
我当前的web.config
< system.web>
< authentication mode =表单>
< forms name =。ADAuthCookie loginUrl =〜/ MainMenu / Login timeout = 45
slideExpiration = 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>
<添加名称= ADConnectionString
connectioString = LDAP://myserver.mydomain.COM:389 / DC = mydomain,DC = COM />
< / connectionStrings>
Leo
我这样解决了设置视图模型和控制器的问题:
模型
公共类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
这篇关于使用ASP.NET MVC中的活动目录进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!