简体   繁体   中英

Asp.net core 2 - External Authentication in WebApi

Now I know there are lots of post about this already but my case is different. So please bear with me.

When I create a WebApi on .Net Framework and choose Individual User accounts, it already scaffolds me a working code for external authentication. 在此输入图像描述

There is AccountController, Startup.auth.cs and other files that has code for External authentication 在此输入图像描述

Now i know things have changed for .net core. But net.core web api on individual user accounts provide nothing similar to it.

When I try to do the same with WebApi on .net Core, it provides me only one option to choose some Azure application. In the picture below, with WebApi on .net core, that dropdown pointed by blue only provides one option which is Azure AD b2c application. No Account controller is scaffolded and no other configuration files for external authentication as well.

在此输入图像描述

No files that I need. 在此输入图像描述

I have tried these links: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/external-authentication-services

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/

But first link is for .net framework that doesnot help me. The second only works for Web application on asp.net core but NOT WebApi

I need external authentication in web api because I want all my users from Web or mobile to be authenticated by Google or facebook. Anybody please point me in right direction. Am I missing something here. Are there any documents that can help me with this?

When selecting the "Individual User Accounts" you want to select "Store user accounts in-app".

When creating the app this way, the scaffolding will create the AccountController. After you created your project this way you can head over to Startup.cs

In the ConfigureServices Method you have to add (for google):

services.AddAuthentication()
    .AddGoogle(options => 
    {
        //Set client Id and secret here
        options.ClientId = "clientId_here";
        options.ClientSecret = "ClientSecret_here";
    });

For more info check out: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/

You can use JSON Web Tokens, or JWT.

Here is an article to guide to the right path: https://auth0.com/blog/securing-asp-dot-net-core-2-applications-with-jwts/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM