根据这篇帖子http://www.asp.net/web-api/overview/security/external-authentication-services. 我可以使用local身份验证服务登录(使用新的ASP.NET身份框架)
但我找不到一个演练来正确调用(从移动应用程序或Postman)Visual Studio 2013 SPA模板中生成的默认web API.
有人能帮我吗?
根据这篇帖子http://www.asp.net/web-api/overview/security/external-authentication-services. 我可以使用local身份验证服务登录(使用新的ASP.NET身份框架)
但我找不到一个演练来正确调用(从移动应用程序或Postman)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=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1",
"state":"QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1"}]
现在,向要使用的Provider 的url发送GET请求.您将被重定向到外部Provider 的登录页面.填写您的凭据,您将被重定向回您的网站.现在从url解析access_token
.
http://localhost:15359/#access_token=[..]&token_type=bearer&expires_in=[..]&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
如果用户已经拥有本地帐户,则设置.AspNet.Cookies
Cookie,您就完成了.如果不是,则只设置.AspNet.ExternalCookie
Cookie,并且您必须注册本地帐户.
有一个接口可以查看用户是否注册:
GET /api/Account/UserInfo
答案是
{"userName":"xxx","hasRegistered":false,"loginProvider":"Facebook"}
要为用户创建本地帐户,请致电
POST /api/Account/RegisterExternal
Authorization: Bearer VPcd1RQ4X... (access_token from url)
Content-Type: application/json
{"UserName":"myusername"}
现在,使用与之前相同的提供者url发送相同的请求
GET /api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
但这一次,用户已经拥有了一个帐户,并获得了身份验证.你可以再次拨打/api/Account/UserInfo
来验证这一点.
现在从url中提取access_token
.你必须在每个请求中添加Authorization: Bearer [access_token]
头.