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

时间:2016-11-02 20:07:48

标签: asp.net-mvc openid openid-connect

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

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

开箱即用的模板允许外部类型的其他登录选项通过_ExternalLoginsListPartial显示在Login视图中,但OpenId Connection没有列在那里。

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

    }

1 个答案:

答案 0 :(得分:1)

看起来我需要做的就是将app.UseOpenIdConnectAuthentication代码放在​​其他身份验证选项之前。这允许默认显示帐户/登录表单,并显示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);


    }