【问题标题】:MVC3 Custom MembershipProvider & Active DirectoryMVC3 自定义 MembershipProvider 和 Active Directory
【发布时间】:2012-01-15 10:48:22
【问题描述】:

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

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

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

【问题讨论】:

  • 你最后找到解决办法了吗?

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


【解决方案1】:

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

让我们看看从哪里开始

在我的 Web 应用程序中,我直接使用我的 ADFS 服务器设置了基于联合声明的身份验证。我还没有找到一个很好的教程来说明如何做到这一点,因为它不是微不足道的。但是有很多关于如何使用 azure ACS 作为中间人来做到这一点的参考资料。这至少会让你开始:

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

一旦你得到这个工作,你只需要几件事。

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

我还利用继承来进行身份验证。我所有的控制器都从 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);
    }

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2018-02-01
    • 2020-02-19
    相关资源
    最近更新 更多