asp.net-mvc-3 - MVC3 自定义 MembershipProvider 和 Active Directory

标签 asp.net-mvc-3 active-directory custom-membershipprovider

我有一个 MVC3 应用程序,它具有存储在数据库中的自定义成员资格提供程序和用户/角色。用户是根据需要在应用程序中手动创建的,并分配了适当的角色。

我现在想扩展应用程序以提供使用 Active Directory 的选项,尽管因为应用程序有几个自定义字段 + 用户 FK 查找的表,我认为我仍然必须有一个自定义默认事件目录成员资格提供程序的版本。

SF 上有没有人做过类似的事情可以与我分享? 谢谢

最佳答案

我知道这是一个老问题但是......

让我们看看从哪里开始

在我的网络应用程序中,我直接使用我的 ADFS 服务器设置了基于联合声明的身份验证。我一直无法找到关于如何执行此操作的好教程,因为它并不简单。但是有很多关于如何使用 azure ACS 作为中间人来做到这一点的引用资料。这至少会让你开始:

http://haishibai.blogspot.com/2011/05/tutorialaspnet-mvc-3-claim-based.html

一旦你开始工作,你只需要一些东西。

在您的数据库用户表上添加几个可以与 AD 链接的属性。我将 AD GUID 存储在我的中,但我也使用电子邮件地址作为辅助地址。这允许我在我的应用程序中创建用户,然后让他们使用 AD 进行身份验证。我只是将他们的电子邮件作为声明传回,将他们与我的应用程序中的用户匹配,然后将 AD GUID 添加到用户。

我还利用继承来进行身份验证。我的所有 Controller 都继承自 BaseController,因此它们具有此标准行为。

public class BaseController
{
    protected override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        if (filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            //read in the claims that we got back from ADFS
            IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;
            IClaimsIdentity ici = icp.Identity as IClaimsIdentity;

            var claims = ici.Claims;

            // This is a claim that I add manually to see if I've already synced
            // ADFS user with DB user
            var ppid = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.PPID);
            if (ppid == null)
            {
                //query/sync user.
                var guidString = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Name).Value;

                // get AD GUID 
                var userGuid = new Guid(System.Convert.FromBase64String(guidString));

                //look up user
                var currentUser = UserRepository.FetchUserByGUID(userGuid);

                //if user not found try fetch by email.
                if (currentUser == null)
                {
                    var email = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Email).Value;
                    currentUser = UserRepository.FetchByEmail(email);
                }

                //If user is still not found create User
                if (currentUser == null)
                {
                    currentUser = new Models.User();
                    BaseRepository.GetDataContext().Users.Add(currentUser);
                }

                //update users information using AD claim as master record
                currentUser.ADID = userGuid;
                currentUser.Name = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.GivenName).Value;
                currentUser.EmailAddress = claims.FirstOrDefault(x => x.ClaimType == ClaimTypes.Email).Value;
                currentUser.LastLoginDate = DateTime.UtcNow;
                currentUser.LoginCount = currentUser.LoginCount + 1;

                BaseRepository.GetDataContext().SaveChanges();

                // Now that you have your AD user linked to your user record 
                // in your database...
                // Create new claims in your ADFS token that include all the roles that
                // your user has.  That way you can just piggyback on claims based 
                // authentication
                foreach (var r in currentUser.Roles)
                {
                    claims.Add(new Claim(ClaimTypes.Role, r.Name));
                }

                // Add userid claim so that we know that this users claims have already
                // been sync with my database
                claims.Add(new Claim(ClaimTypes.PPID, currentUser.Id.ToString()));
            }
        }

        base.OnAuthorization(filterContext);
    }

希望对您有所帮助!

关于asp.net-mvc-3 - MVC3 自定义 MembershipProvider 和 Active Directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869116/

相关文章:

asp.net-mvc - ASP.NET MVC 4 - 嵌套模型 - 如何设置正确的关联?

javascript - 在 Javascript 对象属性中添加 HTML 的简洁方法

asp.net-mvc-3 - 审查 Orchard 项目

Azure ACS、WIF 3.5、Asp.Net 3.5 自定义成员资格提供程序和 IsAuthenticated

c# - Razor 智能感知错误 : Feature 'extension method' cannot be used because it is not part of the ISO-2 C# language specification

jsp - 使用 Tomcat 6 中的 server.xml 连接时如何从 Active Directory 获取电子邮件

PowerShell,异常处理 : Create custom logfile from Set-ADUser

node.js - 使用 Active Directory 进行身份验证/SSO 的外部 Rails/Node 应用程序

c# - web.config 中的成员资格自定义提供程序调用失败