简体   繁体   English

ASP.NET核心Web API和Angular Client中的外部身份验证

[英]External authentication in ASP.NET core Web API and Angular Client

I have an angular application that uses asp.net core Web API. 我有一个使用asp.net核心Web API的角度应用程序。 I need to add external authentication for google and facebook sign in so that my web and mobile application can use this web api to authenticate users. 我需要为google和facebook登录添加外部身份验证,以便我的网站和移动应用程序可以使用此网络API验证用户身份。 I have gone through the documents and tutorials provided for this like this one and also this but none of them are helping me because my web application is on Angular 5. 我已经通过提供这种类似的文件和教程了这一个也是这个 ,但他们都不帮我,因为我的web应用程序是对角5。

My actual problem is how to get URL that redirects me to Google or facebook sign in page. 我的实际问题是如何获取将我重定向到Google或Facebook登录页面的网址。

Mostly all of them use scafolded Asp.net mvc application where there is a controller: AccountController with Methods ExternalLogins 大多数都使用scafolded Asp.net mvc应用程序,其中有一个控制器:AccountController with Methods ExternalLogins

GET /api/Account/ExternalLogins?returnUrl=%2F&generateState=true

and they say the response will be a json like this: 并且他们说响应将是这样的json:

   [{"name":"Facebook",
  "url":"/api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1",
  "state":"QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1"}]

This JSON is exactly what I need because i need a URL like that to open facebook or google login page. 这个JSON正是我需要的,因为我需要这样的URL来打开facebook或谷歌登录页面。 The links above use scaffolded projects and every code written there already that works great for Asp.net web application. 上面的链接使用脚手架项目,并且已经编写的每个代码都适用于Asp.net Web应用程序。 But in my case, I have started the web api from scratch and my web application is Angular 5. Also if I tried a scafolded web api project but I could not find any controller called AccountController, nor any Method called ExternalLogins to get the JSON that contains provider name and login page url. 但在我的情况下,我从头开始创建web api,我的Web应用程序是Angular 5.此外,如果我尝试了一个scafolded web api项目,但我找不到任何名为AccountController的控制器,也没有任何名为ExternalLogins的方法来获取JSON包含提供者名称和登录页面URL。 It looks like the way its done has been changed in .net core 2. The first link above has shown two ways to include GoogleAuthentication in startup class file for Asp.net core 1 and Asp.net Core 2. So there must be something I am missing. 它看起来像是在.net核心2中改变它的方式。上面的第一个链接显示了在Asp.net核心1和Asp.net Core 2的启动类文件中包含GoogleAuthentication的两种方法。所以必须有一些东西我我失踪了。

I am using Asp.net core 2. I have already got Api keys and secrets and added to startup class file. 我使用的是Asp.net核心2.我已经有了Api密钥和秘密,并添加到启动类文件中。

I hope I am clear enough. 我希望我足够清楚。

Please tell me if I need to elaborate more. 请告诉我是否需要详细说明。

Please help. 请帮忙。 Thanks. 谢谢。

You need to create Challenge for Google Authentication in one of your Controller in API and redirect from angular app to that API Controller. 您需要在其中一个Controller in API中创建Google身份验证挑战,并从angular应用程序重定向到该API控制器。

Controller Method 控制器方法

    [HttpGet("ExternalLogin")]
    public IActionResult ExternalLogin(string provider)
    {
        if (provider == null)
        {
            return null;
        }

        var properties = new AuthenticationProperties { RedirectUri = "www.mydomain.com/api/auth/callback" };
        var response = Challenge(properties, provider);
        return response;
    }

component method called on click of google sign in button 单击谷歌登录按钮时调用的组件方法

externalLogin(provider:string){
this.document.location.href = this.baseAPIUrl + 'auth/externallogin?provider='+provider;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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