具有 OpenId 选项的 MVC 个人用户帐户

MVC individual user accounts with OpenId option

我有一个 MVC 5 应用程序,我想允许外部用户创建一个帐户并登录。 此外,我希望员工无需创建帐户即可使用该应用程序。 为此,我将 mvc 站点设置为用户个人用户帐户。 我还在 Startup.Auth 中添加了代码以允许 OpenId Connect 令牌。

问题是,如果我在启动时保留 OpenId 代码,则身份验证默认为该代码。我希望所有用户首先进入 Account/Login 页面,然后让员工选择使用 OpenId。

开箱即用的模板允许通过 _ExternalLoginsListPartial 在登录视图中显示外部类型的额外登录选项,但此处未列出 OpenId 连接。

OpenId 令牌的权限是我们现场的 ADFS 服务器。

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

        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.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

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

        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

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

    }

看来我需要做的就是将 app.UseOpenIdConnectAuthentication 代码放在其他身份验证选项之前。这允许默认显示 Account/Login 表单,并显示 OpenId 按钮以允许该选项。

        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);


    }