首页 文章

Asp.net核心2 - WebApi中的外部身份验证

提问于
浏览
5

现在我知道有很多关于这个的帖子,但我的情况有所不同 . 所以请耐心等待 .

当我在.Net Framework上创建WebApi并选择个人用户帐户时,它已经为我提供了一个外部身份验证的工作代码 .
enter image description here

有AccountController,Startup.auth.cs和其他具有外部认证代码的文件
enter image description here

现在我知道.net核心已经发生了变化 . 但个人用户帐户的net.core web api与它没有任何相似之处 .

当我尝试在.net Core上使用WebApi时,它只为我提供了一个选择Azure应用程序的选项 . 在下图中,使用.net核心上的WebApi,蓝色指向的下拉列表仅提供一个Azure AD b2c应用程序选项 . 没有帐户控制器是脚手架,也没有其他配置文件用于外部身份验证 .

enter image description here

没有我需要的文件 .
enter image description here

我试过这些链接: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/

但第一个链接是针对.net框架,但对我没有帮助 . 第二个仅适用于asp.net核心上的Web应用程序但是 NOT WebApi

我需要在web api中进行外部身份验证,因为我希望所有来自Web或移动设备的用户都可以通过Google或Facebook进行身份验证 . 请任何人指出我正确的方向 . 我在这里错过了一些东西 . 有没有文件可以帮我解决这个问题?

2 回答

  • 0

    选择“个人用户帐户”时,您要选择“在应用程序中存储用户帐户” .

    以这种方式创建应用程序时,脚手架将创建AccountController . 以这种方式创建项目后,您可以转到Startup.cs

    在ConfigureServices方法中,您必须添加(对于谷歌):

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

    欲了解更多信息,请访问:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/

  • 0

    您可以使用JSON Web Tokens或JWT .

    这是一篇指导正确路径的文章:https://auth0.com/blog/securing-asp-dot-net-core-2-applications-with-jwts/

相关问题