繁体   English   中英

使用OpenId Connect与Azure AD然后模拟现有的身份用户

[英]Using OpenId Connect with Azure AD then impersonate an existing Identity User

我正在将Azure AD身份验证添加到现有的MVC应用程序中,在该应用程序中,它使用OWIN Identity从SQL数据库对用户进行身份验证。

数据库用户在具有特定规则的复杂业务中用于管理应用程序的某些部分。

我已经使用Azure实现了OpenId Connect并且能够登录但是我需要使用已经存在的身份映射AD身份验证用户然后模拟此身份以使用...登录。

一旦AD身份验证成功,我就将这个控制器重定向到了:

现在应用程序确实重定向,但身份保持从AD而不是身份一。

这段代码有什么问题? 我该怎么解决?

if (Request.IsAuthenticated)
            {
                //The AD Name Identity is the user email
                var email = User.Identity.Name;

                //Get the user from Identity Provider
                var user = UserManager.Users.FirstOrDefault(t => t.Email == email);

                if(user!= null)
                {
                    //Sign In and Authenticate User
                    var identity = UserManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    var authenticationManager = HttpContext.GetOwinContext().Authentication;

                    authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);

                    var authenticationOptions = new CookieAuthenticationOptions
                    {
                        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
                    };

                    var app = HttpContext.GetOwinContext().Get<AppBuilderProvider>().Get();
                    app.UseCookieAuthentication(authenticationOptions);

                    return RedirectToAction("Index", "Dashboard");
                }

                return RedirectToAction("Index", "Dashboard");
            }

如果您尝试将AD帐户与本地帐户关联或链接,请查看新Web项目中的默认ASP.net身份代码。 它使用本地帐户和任意数量的外部帐户来跟踪和管理用户。 如果没有为您解决问题,这应该让您朝着正确的方向前进。

但是,如果您要模拟登录的其他用户(例如,用于调试),那么您可以实现该用户。

这是一种方式:当您使用外部提供程序进行身份验证时,用户将被重定向回您的控制器(通常在AccountController中通常称为外部登录或其他操作)。

默认情况下,此操作将使用外部提供商的声明信息向用户签名。

此时,您可以选择实现“模拟”的方式。

•您可以允许用户根据权限通过将其注销并以所选用户身份登录来模拟其他用户。 •您可以将模拟用户标识存储为排序的会话变量,并将其作为用户进行检查(如果不存在则返回实际用户)

你如何实施模仿取决于你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM