ASP.NET Web API 2:如何使用外部身份验证服务登录? [英] ASP.NET Web API 2: How do I log in with external authentication services?
问题描述
根据此帖子 http://www.asp .net / web-api / overview / security / external-authentication-services ...
我可以使用 local 身份验证服务(使用新的ASP.NET身份框架)
,但是我找不到演练来正确调用(从移动应用程序或邮递员)是Visual Studio 2013 SPA模板中生成的默认Web API。
有人可以帮助我吗?
我今天遇到了同样的问题,找到了以下解决方案:
首先获取所有可用的提供程序
GET / api / Account / ExternalLogins?returnUrl =%2F& generateState = true
响应消息是json格式的列表
[{ name: Facebook,
url: / api / Account / ExternalLogin?provider = Facebook& response_type = token& client_id = self& redirect_uri = http%3A%2F%2Flocalhost%3A15359%2F& state = QotufgXRptkAfJvcthIOWBnGZydgVkQr $$ : QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1}]
现在将GET请求发送到您要使用的提供者的URL 。您将被重定向到外部提供商的登录页面。填写您的凭据,您将被重定向回您的站点。现在,从网址中解析 access_token
。
http:// localhost :15359 /#access_token = [..]& token_type = bearer& expires_in = [..]& state = QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
b $ b
如果用户已经有一个本地帐户,则设置 .AspNet.Cookies
cookie即可完成。如果没有,则仅设置 .AspNet.ExternalCookie
cookie,并且您必须注册一个本地帐户。
有是一个用于查找用户是否已注册的api:
GET / api / Account / UserInfo
响应是
{ userName: xxx, hasRegistered:false, loginProvider: Facebook}
要为该用户创建本地帐户,请调用
POST / api / Account / RegisterExternal
授权:Bearer VPcd1RQ4X ...(来自URL的access_token)
内容类型:application / json
{ UserName: myusername}
现在使用与提供者网址相同的请求发送
GET / api / Account / ExternalLogin?provider = Facebook& response_type = token& client_id = self& redirect_uri = http%3A%2F%2Flocalhost%3A15359%2F& state = QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8Yr $ b $ c $ p $ c $ p $ c >
但是这一次用户已经有一个帐户并获得了身份验证。您可以通过再次调用 / api / Account / UserInfo
进行验证。
现在提取 access_token
。您必须在每个请求中添加 Authorization:Bearer [access_token]
标头。
According to this post http://www.asp.net/web-api/overview/security/external-authentication-services...
I'm able to log in with a local authentication service (with the new ASP.NET identity framework)
but I can't find a walkthrough to properly call (from a mobile app or Postman) the default web API generated in the Visual Studio 2013 SPA template.
Can anyone help me?
解决方案 I had the same problem today and found the following solution:
At first get all available providers
GET /api/Account/ExternalLogins?returnUrl=%2F&generateState=true
The response message is a list in json format
[{"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"}]
Now send a GET request to the url of the provider you want to use. You will be redirected to the login page of the external provider. Fill in your credentials and the you will be redirected back to your site. Now parse the access_token
from the url.
http://localhost:15359/#access_token=[..]&token_type=bearer&expires_in=[..]&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
If the user already has a local account, the .AspNet.Cookies
cookie is set and you are done. If not, only the .AspNet.ExternalCookie
cookie is set and you have to register a local account.
There is an api to find out if the user is registered:
GET /api/Account/UserInfo
The response is
{"userName":"xxx","hasRegistered":false,"loginProvider":"Facebook"}
To create a local account for the user, call
POST /api/Account/RegisterExternal
Authorization: Bearer VPcd1RQ4X... (access_token from url)
Content-Type: application/json
{"UserName":"myusername"}
Now send the same request with the provider url as before
GET /api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
But this time the user already has an account and gets authenticated. You can verify this by calling /api/Account/UserInfo
again.
Now extract the access_token
from the url. You have to add the Authorization: Bearer [access_token]
header to every request you make.
这篇关于ASP.NET Web API 2:如何使用外部身份验证服务登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!