2017-03-24 6 views
3

私は、Google+ログインでユーザーを認証する必要のあるWebビューで.NET Coreアプリケーションを作成しています。 この(https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins)チュートリアルに従って、ユーザーにGoogleのGoogleアカウントをサインインできるようになりました。ここまでは順調ですね。Asp.Net Core google-signin oauthアクセスを制限してgスイートの役割を取得

アプリケーションへのアクセスを特定のドメイン内のユーザーのみに制限するにはどうすればよいですか。

gスイートで定義された認証済みユーザーロールを取得するにはどうすればよいですか?

Startup.csの認証オプションにスコープを追加しようとしましたが、その中に含まれている情報の抽出/受信方法がわかりません。

 app.UseGoogleAuthentication(new GoogleOptions() 
     { Scope = 
      { "https://www.googleapis.com/auth/admin.directory.domain.readonly", 
       "https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly" 
      }, 
      ClientId = Configuration["Authentication:Google:ClientId"], 
      ClientSecret = Configuration["Authentication:Google:ClientSecret"] 
     }); 

答えて

3

まあ、私の自己を最後に解決しました。他の誰かがこの問題を抱えている場合にこの答えを残してください。 AccountController.ExternalLoginCallbackから

コードスニペット()

var info = await _signInManager.GetExternalLoginInfoAsync(); 
    foreach (var claim in info.Principal.Claims) 
    { 
     System.Diagnostics.Debug.WriteLine("Type: " + claim.Type + " Value: " + claim.Value); 
    } 

これは、ロールとドメイン(HD)が定義されている特許請求の範囲のリストを表示します。

または、ミドルウェア(Startup.configure())での主張を取得できます。

app.UseGoogleAuthentication(new GoogleOptions() 
    { 
     Scope = 
     { "https://www.googleapis.com/auth/admin.directory.domain.readonly", 
      "https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly" 
     }, 
     ClientId = Configuration["Authentication:Google:ClientId"], 
     ClientSecret = Configuration["Authentication:Google:ClientSecret"], 
     Events = new OAuthEvents() 
     { 
      OnTicketReceived = e => 
      { 
       var claims = e.Principal.Claims; 
       // Do something with claims 
       return Task.CompletedTask; 
      } 
     } 
    }); 
関連する問題