C# 在asp.net MVC中使用active directory进行身份验证
我想使用active directory对我的asp.net mvc项目中的用户进行身份验证,在花了好几个小时上网后,我没有发现任何对我有用的东西,我已经看到了所有结果,但什么都没有 我试着按照许多帖子的建议编辑我的web.config 如果有人能在代码或示例方面帮助我,我会非常感激,因为我不知道从哪里开始 编辑 我当前的web.configC# 在asp.net MVC中使用active directory进行身份验证,c#,asp.net-mvc,active-directory,C#,Asp.net Mvc,Active Directory,我想使用active directory对我的asp.net mvc项目中的用户进行身份验证,在花了好几个小时上网后,我没有发现任何对我有用的东西,我已经看到了所有结果,但什么都没有 我试着按照许多帖子的建议编辑我的web.config 如果有人能在代码或示例方面帮助我,我会非常感激,因为我不知道从哪里开始 编辑 我当前的web.config <system.web> <authentication mode="Forms"> <forms name=".ADA
<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如评论中所述,将身份验证方法从
表单
更改为Windows
:
<authentication mode="Windows" />
或者,要限制特定广告用户的访问,请执行以下操作:
[Authorize(Users="DOMAIN\UserName")]
可以使用逗号分隔添加多个角色或用户:
[Authorize(Roles="DOMAIN\Group1, DOMAIN\Group2")]
这些属性可以应用于整个控制器或单个动作
更多信息。如果您需要根据AD验证用户提交的用户名和密码,请使用以下代码 添加对
System.DirectoryServices.AccountManagement的引用
在代码文件中
using System.DirectoryServices.AccountManagement;
接下来,在POST方法中(标志是布尔类型变量)
如果标志为true,则凭据有效,否则无效
在这种情况下,您需要手动设置用户和角色(通过使用表单身份验证),然后您可以通过使用来限制用户/角色显示控制器/视图
[Authorize(Roles="RoleName")]
我解决了如下设置视图模型和控制器的问题:
模型
控制器
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");
}
看法
@使用(Html.BeginForm(“LogOn”,null,FormMethod.Post))
{
@Html.ValidationSummary(true,“”)
@EditorFor(model=>model.UserName)
@Html.ValidationMessageFor(model=>model.UserName,“,)
@EditorFor(model=>model.Password)
@Html.ValidationMessageFor(model=>model.Password,“”)
@CheckBoxFor(model=>model.RememberMe)
登录
}
web.config与我的问题保持一致Ciao Leo,请看,您只需设置@Max发布的
注释即可解决您的问题。但是,是否要根据active directory对用户提交的凭据进行身份验证???@GaganDeep是,特别是我要向属于特定用户的用户显示视图group@Max只添加标签?没有代码了?例如控制器、视图..?答案(标签)提供的解决方案解决了问题,无需询问用户的凭据,只需获取当前窗口用户。我有点困惑,但我需要一种表单,用户必须插入用户名并传递,验证凭据后,用户只能在某些部分导航,例如查看某些内容的详细信息但不能编辑,例如:示例用户,而管理员可以执行所有操作。一种后端,允许指示哪些域用户可以使用特定权限访问。使用用户必须输入用户名和密码的表单的唯一原因是,您的服务器没有加入到与用户相同的域(或受信任的域)。这就是你的情况吗?没有人,我的电脑加入了服务器的同一个域,然后使用Windows身份验证:)这将使你的生活变得更加轻松。它会为您验证他们的凭据,您只需使用[Authorize]
属性锁定所需的控制器或操作。很好,但经过一些考虑后,我得出结论,我还需要使用不在域中的其他用户或pc登录网站,如果您知道我可以从哪里开始,我将不胜感激:)这段代码是为控制器编写的,对吗?它从我的登录视图接收用户名并通过Post,这种方法正确吗?
[Authorize(Roles="RoleName")]
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; }
}
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>
}