简体   繁体   English

处理OAuth 2.0身份验证 - 在ASP.NET MVC应用程序中获取令牌重定向令牌响应

[英]Handle OAuth 2.0 Authentication - Get token redirect token response in ASP.NET MVC application

I recently started fiddling with OneDrive API which uses OAuth 2.0 authentication / authorization flow. 我最近开始摆弄使用OAuth 2.0身份验证/授权流程的OneDrive API。

OneDrive API OAuth 2.0 OneDrive API OAuth 2.0

I am trying to follow along the Token Flow to get access token using an ASP.NET MVC Application. 我试图跟随Token Flow使用ASP.NET MVC应用程序获取访问令牌。

The request I make is something similar to the following: 我提出的请求类似于以下内容:

GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}&response_type=token&redirect_uri={redirect_uri}

On successful authorization from the user the web browser will be redirected to your redirect URL with additional parameters added to the URL . 在用户成功授权后,Web浏览器将重定向到您的重定向URL, 并在URL中添加其他参数。

The redirect URL is similar to the following: https://login.live.com/oauth20_authorize.srf#access_token={access_token}&authentication_token={authentication_token}&token_type=bearer&expires_in=3600&scope=onedrive.info%20onedrive.full&user_id={user_id} 重定向URL类似于以下内容: https://login.live.com/oauth20_authorize.srf#access_token={access_token}&authentication_token={authentication_token}&token_type=bearer&expires_in=3600&scope=onedrive.info%20onedrive.full&user_id={user_id}https://login.live.com/oauth20_authorize.srf#access_token={access_token}&authentication_token={authentication_token}&token_type=bearer&expires_in=3600&scope=onedrive.info%20onedrive.full&user_id={user_id}

Notice the # in the above format URL. 请注意上述格式URL中的# This is as per specification of OAuth 2.0 protocol standards. 这是根据OAuth 2.0协议标准的规范。

Question: If I have a redirect URL to handle in my MVC application which has query parameters section starting with ? 问题:如果我在我的MVC应用程序中有一个重定向URL,其查询参数部分以?开头? then it can be handled very easily using a controller's action with proper input parameters mapping the query string key / values. 然后可以使用控制器的操作非常轻松地处理它,并使用适当的输入参数映射查询字符串键/值。

How can I handle a URL in my MVC application that has query string section starting with # and get all query string key / values from it? 如何处理我的MVC应用程序中的URL,该URL具有以#开头的查询字符串部分并从中获取所有查询字符串键/值?

Side note: I would request not to suggest using any third party API / Libraries / Routines in this case. 附注:在这种情况下,我会要求不建议使用任何第三方API /库/例程。 Considering the fact that I wish to do the OAuth process plain and simple way. 考虑到我希望以简单明了的方式完成OAuth流程。 Thanks 谢谢

The method you have chosen which is the Token Flow also known as the Implicit Grant Flow intentionally uses a # (fragment). 您选择的方法Token Flow也称为Implicit Grant Flow,故意使用#(片段)。 This is for security purposes to avoid man in the middle attacks. 这是为了安全起见,以避免中间人攻击。 The answer in this link provides some description on the topic OAuth2.0 Implicit Grant flow. 此链接中的答案提供了有关OAuth2.0隐式授权流主题的一些说明 Why use url hash fragments? 为什么要使用url哈希片段?

However if you still want to use the Token Flow, you can read the values on the client side in JavaScript using window.location.hash and then process those values with probably an Ajax submit to the server. 但是,如果您仍想使用令牌流,则可以使用window.location.hash在JavaScript中读取客户端上的值,然后将这些值与Ajax提交到服务器。

I would suggest you use the Code Flow which is meant for the scenario you have explained in your question. 我建议您使用代码流,这是针对您在问题中解释的方案。 The Code Flow is available on the same link https://github.com/OneDrive/onedrive-api-docs/blob/master/auth/msa_oauth.md#code-flow 代码流可在同一链接上获得https://github.com/OneDrive/onedrive-api-docs/blob/master/auth/msa_oauth.md#code-flow

Incidentally, the above link has a # (fragment) in it too :) 顺便说一下,上面的链接里面也有#(片段):)

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

相关问题 从OAuth令牌Asp.net MVC获取电子邮件地址 - Get email address from OAuth token Asp.net MVC 删除或过期OAuth令牌Asp.net MVC - Remove or Expire OAuth Token Asp.net MVC Twitter Oauth与ASP.NET MVC,在哪里存储秘密令牌 - Twitter Oauth with ASP.NET MVC, where to store secret token 如何在 Asp.Net MVC 中使用 gmail API 和 Z54699D9A7E6DF4EDBBEZ0 - how to get date wise email faster using gmail API in Asp.Net MVC with OAuth Token 将webApi控制器的令牌认证添加到现有的asp.net MVC 5应用程序 - Add token authentication for webApi controller to existing asp.net MVC 5 application 通过外部小部件 (iframe) 中的令牌在 ASP.NET MVC 应用程序中进行身份验证 - Authentication in an ASP.NET MVC Application via token in an external widget (iframe) 通过ASP.NET MVC中的令牌传递进行身份验证 - Authentication through a token pass in ASP.NET MVC 无法通过ping federate身份验证获得电源双向访问令牌asp.net mvc - unable to get power bi access token asp.net mvc with ping federate authentication [Authorize]属性如何知道用户在ASP.NET MVC中已通过身份验证令牌进行身份验证? - How [Authorize] attribute get to know that the user is authenticate in ASP.NET MVC, is it by using authentication token? 如何从asp.net获取承载令牌作为http响应 - How to get bearer token as http response from asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM