2017-09-01 22 views
0

私はClient ID、redirectUri、Tenantを "Common"として渡したところでAD認証を実装しました。私がlive.com、 "outlook.com"、 "microsoft.com"、 "school and office"から "Common"ユーザーとしてテナントを使用しているので、私はそれをLive.comユーザーだけに制限したい。 Azure AD's v2.0 endpoint docsから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

登録後、アプリがバージョン2.0のエンドポイントにリクエストを送信することにより、AzureのADと通信:

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

{tenant}、1つを取ることができます4つの異なる値:

  • common - 個人用MicrosoftアカウントとAzure Active Directoryの職場/学校アカウントの両方を持つユーザーがアプリケーションにサインインできるようにします。
  • organizationsは - AzureのActive Directoryからの仕事/学校のアカウントを持つユーザーのみがアプリケーション
  • consumersにサインインすることができます - 個人のMicrosoftアカウント(MSA)を持つユーザーのみがアプリケーションにサインインすることができます。
  • 8eaef023-2b34-4da1-9baa-8bc8c9d6a490またはcontoso.onmicrosoft.com- 特定のAzure Active Directoryテナントの職場/学校のアカウントを持つユーザーのみがアプリケーションにサインインできます。 Azure ADテナントのフレンドリドメイン名またはテナントのGUID識別子のいずれかを使用できます。

あなたが消費者のドメインまで、さらに限定したい、(@ live.com対@ outlook.com)場合はあなた自身がアプリケーションレベルでemail主張を見ていることを実装する必要があります。 live.comアカウントとoutlook.comアカウントの間に機能的/実践的な相違がないので、このレベルのフィルタリングは一般的にあまり意味がないことに注意してください。

+1

偉大な答えは、自分のWeb APIを呼び出している場合、あなたのアプリへのアクセスを制限するもう一つの方法を追加する。あなたのWeb APIは発行されたトークンの中の 'iss'クレームを見ることができます。 Microsoftのアカウントユーザーは、ここに存在する固有のテナントIDを使用して、バックエンドがアクセスを検証し制限することができます。トークンのクレームについては、[Azure AD v2.0 token reference](https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-tokens)を参照してください。サンプルのリストについては、[Azure AD v2.0ランディングページ](https://aka.ms/aadv2)を確認してください。 –

+0

すばらしいことです。私はAPI認証にこのAADエンドポイントV2を使用できますか? –

関連する問題