同一MVC应用程序中的OpenID和个人用户帐户

时间:2016-11-01 21:46:27

标签: model-view-controller openid-connect

我希望MVC应用程序支持外部用户的个人用户帐户,并使用OpenID端点为员工接受来自ADFS的令牌。

我创建了2个mvc应用程序。一个是设置为仅使用OpenId,它可以通过将我重定向到本地ADFS服务器并设置cookie来正常工作,因此我被授权使用[Authorize]属性修饰的控制器。

我在与第一个mvc网站相同的服务器上安装了另一个mvc应用程序,该网站设置为使用单个用户帐户。我在Startup.Auth中添加了代码以将OpenId Connect添加到OWIN管道。

      public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });            
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.UseOpenIdConnectAuthentication(
           new OpenIdConnectAuthenticationOptions
           {
               ClientId = clientId,
               MetadataAddress = metadataAddress,
               RedirectUri = redirectUri,
               //PostLogoutRedirectUri = postLogoutRedirectUri
           });

    }

我认为这是我在第二个应用程序中使用OpenID所需的全部内容。 我首先访问第一个mvc(仅限OpenId)应用程序并登录。我可以访问该应用程序上的授权控制器操作。 然后我尝试访问第二个应用程序(个人用户帐户和OpenID),并假设我将被授权。

相反,我在ADFS服务器和mvc应用程序之间重定向几次,直到“Microsoft.IdentityServer.Web.InvalidRequestException:MSIS7042:相同的客户端浏览器会话在最后'1'秒内发出'6'请求。 “错误被抛出。

小提琴曲迹显示:
302 TestApp2
200 adfs服务器
302 TestApp2
302 TestApp2
200 adfs服务器

1 个答案:

答案 0 :(得分:0)

我能够使用此Startup.ConfigureAuth实现此功能。

        public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        app.UseOpenIdConnectAuthentication(
         new OpenIdConnectAuthenticationOptions
         {
             ClientId = clientId,
             MetadataAddress = metadataAddress,
             RedirectUri = redirectUri,
                //PostLogoutRedirectUri = postLogoutRedirectUri
            });

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);


    }