c# - 使用 Active Directory 或成员数据库的 MVC 4 身份验证

标签 c# authentication asp.net-mvc-4 active-directory simplemembership

我正在构建一个可以通过两种方式访问​​的 Web 应用程序。与我在同一个组织工作的每个人都可以使用我们的事件目录来访问该应用程序。

外部的每个人都应该通过单独的成员(member)数据库加入应用程序。每个人都应该在成员(member)数据库中拥有一个帐户和他的角色,因此广告连接只是一个奖励,可以让您更轻松地记住密码和用户名。 我搜索了互联网,但找不到可比的情况。这是我第一次与广告打交道。

有谁知道可以使用的框架或给我提示如何尝试解决问题?

目前我使用 System.Web.WebData.SimpleMembershipProvider 实现了成员连接,它工作正常。

在应用程序的后期开发中,我还需要一些与广告的其他连接来检查一些信息,但这只是另一天的问题。

感谢您的帮助。

最佳答案

打开您的 web.config。

首先,您需要 ActiveDirectory 的 connectionString:

  <connectionStrings>
    ...
    <add name="ADConnectionString" connectionString=LDAP://*adserver*/DC=*domain* />
    ...
  </connectionStrings>

向下滚动到 <membership>标签。确保为 <membership> 设置了 defaultProvider 属性,比如:

<membership defaultProvider="SimpleMembershipProvider">

然后在<providers>内为AD成员添加新的供应商:

    <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />

这应该可以解决 web.config 的问题。现在我们需要在登录时授权 AD 用户。转到您的 AccountController 登录操作。首先我们尝试通过 ActiveDirectory 对用户进行身份验证,有一个名为 PrincipalContext 的方便类在 System.DirectoryServices.AccountManagement命名空间。如果失败,我们将使用默认的成员(member)提供程序:

        public ActionResult Login(LoginModel model, string returnUrl)
        {
            try
            {
                // try to auth user via AD
                using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
                {
                    if (pc.ValidateCredentials(model.UserName, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, false);
                        return RedirectToAction("Index", "Home");
                    }
                }
                // try the default membership auth if active directory fails

                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Login failed");
                }
            }
            catch
            {
            }
            GetErrorsFromModelState();
            return View(model);
        }

对于您以后的需求,您可以使用 UserPrincipal 类获取当前登录的 ActiveDirectory 用户:

using (var context = new PrincipalContext( ContextType.Domain)) 
{
    using (var aduser = UserPrincipal.FindByIdentity( context,IdentityType.SamAccountName, HttpContext.User.Identity.Name))
    {
        ...
    }
}

希望这对您有所帮助,我没有遗漏任何内容。

关于c# - 使用 Active Directory 或成员数据库的 MVC 4 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082807/

相关文章:

javascript - Selenium:如何使 RemoteDriver 始终附加到当前浏览器选项卡?

c# - 将所有基调用包装在派生类型中

c# - 在C#中使用xPath获取节点属性值

iphone - 如何区分没有 Twitter 访问权限的应用程序和没有帐户设置的 Twitter?

jquery - "The decimal field must be a number"

c# - 使用官方 c# 驱动程序在 MongoDB 中按 $natural 排序

java - 谷歌应用引擎用户 API

android - 来自 native Android 应用程序的 Rails 用户身份验证

asp.net-mvc - ASP.NET MVC 自定义路由类 - 无法获取路由数据

asp.net-mvc-4 - DevTrends.MvcDonutCaching.KeyGenerator.GenerateKey 中的 NullReferenceException