【问题标题】:Enable SSO for specific domain users为特定域用户启用 SSO
【发布时间】:2018-02-27 21:30:12
【问题描述】:

我在我的应用程序中使用 OpenID Connect 身份验证。我已在 Microsoft 应用注册门户中注册了我的应用,并从那里收到了客户端 ID 和密码。

private static string appId = ConfigurationManager.AppSettings["ida:AppId"];
private static string appSecret = ConfigurationManager.AppSettings["ida:AppSecret"];
private static string redirectUri = ConfigurationManager.AppSettings["ida:RedirectUri"];
private static string graphScopes = ConfigurationManager.AppSettings["ida:GraphScopes"];

public void ConfigureAuth(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    app.UseOpenIdConnectAuthentication(
        new OpenIdConnectAuthenticationOptions
        {
            ClientId = appId,
            Authority = "https://login.microsoftonline.com/common/v2.0",
            PostLogoutRedirectUri = redirectUri,
            RedirectUri = redirectUri,
            Scope = "openid email profile offline_access " + graphScopes,
            TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = false,
                    // In a real application you would use IssuerValidator for additional checks, 
                    // like making sure the user's organization has signed up for your app.
                    //     IssuerValidator = (issuer, token, tvp) =>
                    //     {
                    //         if (MyCustomTenantValidation(issuer)) 
                    //             return issuer;
                    //         else
                    //             throw new SecurityTokenInvalidIssuerException("Invalid issuer");
                    //     },
            },
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthorizationCodeReceived = async(context) =>
                    {
                        var code = context.Code;
                        string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;

                        TokenCache userTokenCache = new SessionTokenCache(signedInUserID,
                            context.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase).GetMsalCacheInstance();
                        ConfidentialClientApplication cca = new ConfidentialClientApplication(
                            appId,

                            redirectUri,
                            new ClientCredential(appSecret),
                            userTokenCache,
                            null);
                        string[] scopes = graphScopes.Split(new char[] { ' ' });

                        AuthenticationResult result = await cca.AcquireTokenByAuthorizationCodeAsync(code, scopes);
                    },
                    AuthenticationFailed = (context) =>
                    {
                        context.HandleResponse();
                        context.Response.Redirect("/Error?message=" + context.Exception.Message);
                        return Task.FromResult(0);
                    }
            }
        });
}

此代码启用 SSO,但来自任何 Microsoft 帐户,因为我使用了 common 权限。但我希望特定目录或域中的用户登录我的应用程序。

我试过了

Authority = "https://login.microsoftonline.com/{tenant_id}",

而不是

Authority = "https://login.microsoftonline.com/common/v2.0",

但它不起作用,并且浏览器中不显示 Microsoft 登录页面。

【问题讨论】:

    标签: c# azure-active-directory microsoft-graph-api


    【解决方案1】:

    您很接近,但最后缺少/v2.0

    对于您使用的多租户应用(AAD 和 MSA 帐户):

    https://login.microsoftonline.com/common/v2.0
    

    对于单租户应用程序(仅限 AAD),您需要使用:

    https://login.microsoftonline.com/{tenant_id}/v2.0
    

    /v2.0 表示您的应用使用 Azure AD 的“v2.0 应用程序模型”(又名“v2 端点”)。

    【讨论】:

    • 我这边还有一个问题。是否可以仅允许某个域中特定组的用户登录我的应用程序?如果是,那我该怎么做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2016-12-30
    • 2015-03-08
    相关资源
    最近更新 更多