【问题标题】:Windows authentication in MVC 5MVC 5 中的 Windows 身份验证
【发布时间】:2014-07-17 11:57:31
【问题描述】:

我负责为我正在进行的项目实施 Windows 身份验证。我已经查看了所有示例,但似乎没有一个适合我的情况。

拥有有效 Windows 帐户的所有用户都应该能够访问该应用程序,但只有一些被配置为管理员的用户应该能够访问该站点的某些部分。

我认为这是使用的情况:

User.Identity.IsAuthenticated

但这总是返回假。我对此的理解非常有限,这意味着我认为这应该是真的。如何使用他们登录的帐户自动对用户进行身份验证,这样他们就不必输入用户名/密码组合?

我正在使用 MVC 5。

【问题讨论】:

    标签: c# authentication windows-authentication


    【解决方案1】:

    您希望实现 LDAP,并让它与您的 Active Directory 交互

    查看此链接以开始您的工作:

    http://www.schiffhauer.com/mvc-5-and-active-directory-authentication/

    以下是链接中的源代码:

    账户控制人:

    using System.Web.Mvc;
    using System.Web.Security;
    
    using MvcApplication.Models;
    
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return this.View();
        }
    
        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (!this.ModelState.IsValid)
            {
                return this.View(model);
            }
    
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                {
                    return this.Redirect(returnUrl);
                }
    
                return this.RedirectToAction("Index", "Home");
            }
    
            this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
    
            return this.View(model);
        }
    
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
    
            return this.RedirectToAction("Index", "Home");
        }
    }
    

    帐户视图模型:

    using System.ComponentModel.DataAnnotations;
    
    public class LoginModel
    {
        [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; }
    }
    

    最后 web.config 更新:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.web>
          <authentication mode="Forms">
              <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="45" slidingExpiration="false" protection="All" />
          </authentication>
          <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" connectionString="LDAP://primary.mydomain.local:389/DC=MyDomain,DC=Local" />
      </connectionStrings>
    </configuration>
    

    这应该可以帮助您入门,请务必查看链接以了解更多上下文。如果您需要更多帮助,请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2016-03-19
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多