Using Google OAuth to secure web services in aspnet core

Celeste

I'm getting lost in OAuth and OpenIDConnect and aspnet core middleware. Any help on this would be appreciated.

I have multiple UIs (web, native apps) that use the same set of web services, and I'd like to ensure only authenticated users can access the web services. My organization uses Google accounts, so I'd like to use Google authentication restricted to the organization domain.

The web site is properly requiring authentication, following this sample. What I need now is to have the web site (AngularJS 4) invoke my back end web services with an auth token that I can verify with Google.

The back end services are written with aspnet core. I've tried using these approaches: Google middleware and Google OpenIDConnect but these still 1) assume there is a UI that can prompt an unauthorized user to log in, and 2) appear to be cookie-based, and I won't have cookies for the web service calls.

I don't want to prompt the user to log in, since the "user" in this case is a software client. Either they're authenticated or not already. I just need to get the authentication token, validate it, and carry on.

This appears to be the same question, which hasn't been answered yet, either.

Any suggestions are appreciated. Also, suggestions or tips on having native apps do the same!

Celeste

Got it working. As mentioned, I was getting lost, and the OpenIDConnect, though referenced in several areas as a solution, was a red herring for the web services. Here's what is working for me now, with as complete steps as I can provide (some cleanup required):

  1. Add authentication to the UI following these directions
  2. Obtain the JWT token as shown in the first segment here
  3. On each web service call, include the JWT token in the headers:

    Name: Authentication

    Value: Bearer {token value}

  4. Install the JwtBearer NuGet package

  5. In the ConfigureServices method of Startup in the web service, after you AddMvc():

        services.AddAuthorization(options =>
    {   // this policy needed only if you want to restrict to accounts within your domain. otherwise, don't use options. or use whatever options work for you.
            options.AddPolicy("hd",
                policy => policy.RequireAssertion(context =>
                    context.User.HasClaim(c =>
                        c.Type == "hd" &&
                        ("https://accounts.google.com".Equals(c.Issuer) ||
                         "accounts.google.com".Equals(c.Issuer, StringComparison.CurrentCultureIgnoreCase)) &&
                        c.Value == "yourdomain.com"
                    )));
    });
    
  6. In the Configure method, before you UseMvc():

            JwtBearerOptions jwtOptions = new JwtBearerOptions();
            jwtOptions.Audience = "{the OAuth 2.0 client ID credential from google api developer console}";
            jwtOptions.Authority = "https://accounts.google.com";
            jwtOptions.TokenValidationParameters = new TokenValidationParameters();
            jwtOptions.TokenValidationParameters.ValidIssuers = new List<string>()
            {
                "https://accounts.google.com",
                "accounts.google.com"
            };
    
            app.UseJwtBearerAuthentication(jwtOptions);
    

Perhaps there is a more appropriate way to do this...if there is, I'm interested in trying it out. For now, this is working.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Model binding not working in aspnet core web api

分類Dev

[Web API] AspNet.Security.OAuth.BattleNet OAuth2.0の問題

分類Dev

aspnet core 2.2 web app environment variables not changing in docker

分類Dev

Error in using Google Play Services

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

AspNet Core using in memory repo for data protection when running in IIS

分類Dev

unable to use google Oauth with Azure mobile services in android app

分類Dev

Google app engine or amazon web services

分類Dev

Ionic & Google OAuth for mobile app and web site

分類Dev

NLog AspNet Core 5.0

分類Dev

Secure Web Services: REST over HTTPS vs SOAP + WS-Security. Which is better?

分類Dev

Net Core NLog.Web "aspnet-user-identity"は空ですか?

分類Dev

User info using OAuth with Google App Engine

分類Dev

AspNet Core CookieAuthentication with injected SessionStore

分類Dev

Net Core NLog.Webの「aspnet-request:header」プロパティの使用法は?

分類Dev

How to track iteration progress using Rally's Web Services API

分類Dev

Aspnet Core1.1は新しいGoogleサインインに移行します

分類Dev

firebase-core:11.8.0 '' com.google.gms:google-services:3.2.0との競合

分類Dev

OAuth for an API used by multiple services

分類Dev

Unrecognized arguments using oauth2 and Google APIs

分類Dev

Using service accounts and oauth2 to call autoML Google API

分類Dev

using google oauth2.0 with react, express application

分類Dev

Secure method using annotations

分類Dev

Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

分類Dev

Http Query Parameters in UTC in AspNet Core

分類Dev

AspNet Core3ID構成

分類Dev

Getting error after implement 'com.google.firebase:firebase-core:17.0.0' and apply plugin: 'com.google.gms.google-services'

分類Dev

google-services.jsonのother_platform_oauth_clientの目的は何ですか?

Related 関連記事

  1. 1

    Model binding not working in aspnet core web api

  2. 2

    [Web API] AspNet.Security.OAuth.BattleNet OAuth2.0の問題

  3. 3

    aspnet core 2.2 web app environment variables not changing in docker

  4. 4

    Error in using Google Play Services

  5. 5

    AspNet Core using in memory repo for data protection when running in IIS

  6. 6

    AspNet Core using in memory repo for data protection when running in IIS

  7. 7

    AspNet Core using in memory repo for data protection when running in IIS

  8. 8

    unable to use google Oauth with Azure mobile services in android app

  9. 9

    Google app engine or amazon web services

  10. 10

    Ionic & Google OAuth for mobile app and web site

  11. 11

    NLog AspNet Core 5.0

  12. 12

    Secure Web Services: REST over HTTPS vs SOAP + WS-Security. Which is better?

  13. 13

    Net Core NLog.Web "aspnet-user-identity"は空ですか?

  14. 14

    User info using OAuth with Google App Engine

  15. 15

    AspNet Core CookieAuthentication with injected SessionStore

  16. 16

    Net Core NLog.Webの「aspnet-request:header」プロパティの使用法は?

  17. 17

    How to track iteration progress using Rally's Web Services API

  18. 18

    Aspnet Core1.1は新しいGoogleサインインに移行します

  19. 19

    firebase-core:11.8.0 '' com.google.gms:google-services:3.2.0との競合

  20. 20

    OAuth for an API used by multiple services

  21. 21

    Unrecognized arguments using oauth2 and Google APIs

  22. 22

    Using service accounts and oauth2 to call autoML Google API

  23. 23

    using google oauth2.0 with react, express application

  24. 24

    Secure method using annotations

  25. 25

    Use Authorization middleware instead of AuthorizationAttribute ASPNET Core

  26. 26

    Http Query Parameters in UTC in AspNet Core

  27. 27

    AspNet Core3ID構成

  28. 28

    Getting error after implement 'com.google.firebase:firebase-core:17.0.0' and apply plugin: 'com.google.gms.google-services'

  29. 29

    google-services.jsonのother_platform_oauth_clientの目的は何ですか?

ホットタグ

アーカイブ