asp.net-mvc - 具有 OpenId 选项的 MVC 个人用户帐户

标签 asp.net-mvc openid openid-connect

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

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

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

OpenId token 的授权机构是我们的现场 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 代码放在其他身份验证选项之前。这允许默认显示帐户/登录表单,并显示 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);


    }

关于asp.net-mvc - 具有 OpenId 选项的 MVC 个人用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40388852/

相关文章:

c# - 对特定 Google Apps 域的 MVC4/Google OpenID 限制

php - 如何确保我的网站始终以 www.mysite.com 而不是 mysite.com 打开 - 这搞乱了我的 Google OpenID 登录?

iphone - 通过 android 和 iPhone 应用程序集成 yahoo、Google 和 openid?

c# - 包含访问 token 时如何从客户端应用程序中的 IdentityServer 获取用户 ID?

asp.net-mvc - MVC 路由 Html.ActionLink 使用 ?id=1 而不是/id 创建 URL

jquery - 获取由 Jquery mvc4 中的 foreach 循环迭代的 div 内的值?

c# - Web Api授权

asp.net-mvc - 不允许使用用于访问路径'/Documents/TestNote/Documents/AddNote'的HTTP动词POST

reactjs - React keycloak 集成不会重定向到登录页面

azure - 避免 Azure AD 颁发的 JWT token 中包含个人信息