- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据这篇文章http://www.asp.net/web-api/overview/security/external-authentication-services ...我能够使用本地身份验证服务(使用新的 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"}]
现在向您要使用的提供商的 URL 发送 GET 请求。您将被重定向到外部提供商的登录页面。填写您的凭据,您将被重定向回您的网站。现在从 URL 中解析 access_token
。
http://localhost:15359/#access_token=[..]&token_type=bearer&expires_in=[..]&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1
如果用户已有本地帐户,则设置 .AspNet.Cookies
cookie,然后就完成了。如果没有,则仅设置 .AspNet.ExternalCookie
cookie,并且您必须注册本地帐户。
有一个API可以查明用户是否注册:
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]
header 添加到您发出的每个请求中。
关于ASP.NET Web API 2 : How do I log in with external authentication services?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21065648/
我是一名优秀的程序员,十分优秀!