2017-09-01 48 views
0

我已經實現了AD身份驗證,其中我已通過Client ID,redirectUri和Tenant作爲「Common」。由於我使用Tenant作爲live.com的「普通」用戶,因此允許使用outlook.com,microsoft.com以及學校和辦公室。我希望它僅限於Live.com用戶。Microsoft Active Directory身份驗證單租戶「Live.com」

public class Startup 
{ 
    // The Client ID is used by the application to uniquely identify itself to Azure AD. 
    string clientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"]; 

    // RedirectUri is the URL where the user will be redirected to after they sign in. 
    string redirectUri = System.Configuration.ConfigurationManager.AppSettings["RedirectUri"]; 

    // Tenant is the tenant ID (e.g. contoso.onmicrosoft.com, or 'common' for multi-tenant) 
    static string tenant = System.Configuration.ConfigurationManager.AppSettings["Tenant"]; 

    // Authority is the URL for authority, composed by Azure Active Directory v2 endpoint and the tenant name (e.g. https://login.microsoftonline.com/contoso.onmicrosoft.com/v2.0) 
    string authority = "https://login.microsoftonline.com/common/v2.0" ; 

    /// <summary> 
    /// Configure OWIN to use OpenIdConnect 
    /// </summary> 
    /// <param name="app"></param> 
    public void Configuration(IAppBuilder app) 
    { 
     app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
     app.UseOpenIdConnectAuthentication(

     new OpenIdConnectAuthenticationOptions 
     { 
      // Sets the ClientId, authority, RedirectUri as obtained from web.config 
      ClientId = clientId, 
      Authority = authority, 
      RedirectUri = redirectUri, 
      // PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page 
      PostLogoutRedirectUri = "https://localhost:44368/Claims/Register", 
      Scope = OpenIdConnectScopes.OpenIdProfile, 
      // ResponseType is set to request the id_token - which contains basic information about the signed-in user 
      ResponseType = OpenIdConnectResponseTypes.IdToken, 
      // ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application 
      // To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name 
      // To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter 
      TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = false }, 

      // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method 
      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       AuthenticationFailed = OnAuthenticationFailed, 
       AuthorizationCodeReceived = (c) => { 
        var code = c.Code; 
        return Task.FromResult(0); 
       } 
      } 
     } 
    ); 
    } 

    /// <summary> 
    /// Handle failed authentication requests by redirecting the user to the home page with an error in the query string 
    /// </summary> 
    /// <param name="context"></param> 
    /// <returns></returns> 
    private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) 
    { 
     context.HandleResponse(); 
     context.Response.Redirect("/?errormessage=" + context.Exception.Message); 
     return Task.FromResult(0); 
    } 
} 
} 

回答

1

Azure AD's v2.0 endpoint docs

註冊後,應用程序通過發送請求到V2.0端點Azure的AD通信:

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token

{tenant}可以採取一個有四種不同的值:

  • common - 允許用戶使用Azure Active Directory中的個人Microsoft帳戶和工作/學校帳戶登錄到應用程序。
  • organizations - 只允許用戶從Azure的Active Directory的工作/學校帳戶登錄到應用
  • consumers - 只允許用戶與個人賬戶微軟(MSA)登錄到應用程序。
  • 8eaef023-2b34-4da1-9baa-8bc8c9d6a490contoso.onmicrosoft.com - 只允許具有來自特定Azure Active Directory租戶的工作/學校帳戶的用戶登錄到應用程序。可以使用Azure AD租戶的友好域名或租戶的guid標識符。

如果您想進一步降到消費者域(@ live.com VS @ outlook.com)限制你需要實現自己在應用層面看email要求。請注意,通常這種級別的過濾沒有多大意義,因爲live.com帳戶和outlook.com帳戶之間沒有功能/實用差異,他們只是有一個不同的虛榮域。

+1

好的答案是,如果它正在調用自己的Web API,請添加一種方法來限制對您的應用程序的訪問。您的Web API可以在發佈的令牌內查找'iss'聲明。 Microsoft帳戶用戶將擁有一個唯一的租戶ID,您的後端可以驗證和限制訪問權限。檢出[Azure AD v2.0令牌參考](https://docs.microsoft.com/zh-cn/azure/active-directory/develop/active-directory-v2-tokens)以獲取有關令牌聲明的幫助。有關示例的列表,請查看[Azure AD v2.0登錄頁面](https://aka.ms/aadv2)。 –

+0

偉大的,我想要的。我可以使用這個AAD端點V2進行API認證。 –

相關問題