asp.net网站API 2:如何登录与外部认证服务? [英] asp.net web api 2: how to login with external authentication services?

查看:370
本文介绍了asp.net网站API 2:如何登录与外部认证服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个帖子<一个href="http://www.asp.net/web-api/overview/security/external-authentication-services">http://www.asp.net/web-api/overview/security/external-authentication-services... 我能够登录使用的本地的身份验证服务(用新的Asp.net的身份框架)

但无法找到一个walktrough正确地调用(从一个移动应用程序或邮差)在Visual Studio生成的默认Web API 2013 SPA模板。

谁能帮助我?

解决方案

今天我有同样的问题,发现了以下解决方案:

首先获得所有可用的供应商

  GET / API /帐号/ ExternalLogins RETURNURL =%2F&放大器; generateState =真
 

响应消息是JSON格式列表

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

现在发送GET请求,要使用提供的网址。您将被重定向到外部提供商的登录页面。在您的凭据填写和你将被重定向到你的网站。现在从URL解析 access_token

<$p$p><$c$c>http://localhost:15359/#access_token=[..]&token_type=bearer&expires_in=[..]&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1

如果用户已经有一个本地帐户,该 .AspNet.Cookies Cookie设置和你做。如果没有,只有 .AspNet.ExternalCookie Cookie设置,你必须注册一个本地帐户。

有一个API来看看用户注册:

  GET / API /帐号/的UserInfo
 

的响应是

  {username的:XXX,hasRegistered:假的,loginProvider:脸谱}
 

要为用户创建一个本地帐户,电话

  POST / API /帐号/ RegisterExternal
授权:承载VPcd1RQ4X ...(从网址access_token)
内容类型:应用程序/ JSON
{用户名:名为myusername}
 

现在才

发送相同的请求与提供商网址

  GET /api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
 

但此时用户已经有一个帐户,并通过认证。您可以通过调用验证这一点 / API /帐号/的UserInfo 了。

现在从URL中提取 access_token 。你要添加授权:承载[access_token] 头的每一个要求你做

according to this post http://www.asp.net/web-api/overview/security/external-authentication-services... i'm able to login with local authentication service (with new Asp.net identity framework)

but can't find a walktrough to call properly (from a mobile app or Postman ) the default web api generated in 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网站API 2:如何登录与外部认证服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆