在ASP.NET MVC中的三足oauth 2.0流中未收到access_token(Blackboard Learn)

时间:2019-02-08 09:39:15

标签: blackboard

我必须在ASP.NET MVC中实现三足身份验证。我已经按照Blackboard文档进行了操作,尤其是链接https://community.blackboard.com/docs/DOC-3976-three-legged-oauth

我已经通过调用REST API / learn / api / public / v1 / oauth2 / authorizationcode收到了授权代码。之后根据文档(我严格按照文档进行操作,但我不知道自己缺少什么),我向/ learn / api / public / v1 / oauth2 / token建立了POST请求以获取access_token,但无法获取access_token。

相反,access_token,我一直在收到BadRequest。这意味着我在建立第二个请求时出错,但是无法解决问题。我没有在.NET中找到任何代码示例来实现Blackboard Learn的三足身份验证。您能帮我解决问题吗?

这是我的代码,用于调用两个API来接收access_token。

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // GET /learn/api/public/v1/oauth2/authorizationcode

            Guid stateId = Guid.NewGuid();

            string applicationKey = "Application key goes here";

            string redirectUrl = string.Format("https://Blackboard Learn URL goes here/learn/api/public/v1/oauth2/authorizationcode" +

            "?redirect_uri=https://localhost:44300/Home/OAuth2Response&response_type=code&client_id={0}&scope=read&state={1}",

            applicationKey, stateId);

            Response.Redirect(redirectUrl, true);

            return View();
        }


        public async Task<bool> OAuth2Response(string code = null, string state = null, string error = null, string error_description = null)    
        {    
            bool success = true;  

            string json = string.Empty;

            string urlCommand = string.Format("/learn/api/public/v1/oauth2/token?code={0}&redirect_url=https://localhost:44300/Home/OAuth2Response", code);

            try    
            {    
                using (HttpClient client = new HttpClient())    
                {    
                    var endpoint = new Uri("Blackboard Learn URL goes here" + urlCommand);


                    var postData = new List<KeyValuePair<string, string>>();

                    postData.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));    

                        HttpContent body = new FormUrlEncodedContent(postData);       

                    // POST /learn/api/public/v1/oauth2/token

                    using (HttpResponseMessage response = await client.PostAsync(endpoint, body)) // Problem is here    
                    {    
                        if (response.IsSuccessStatusCode)

                        {

                            json = await response.Content.ReadAsStringAsync();

                        }

                        else

                        {

                            success = false;

                        }

                    }

                }

            }

            catch (Exception err)

            {

                //hopefully we never end up here, log this exception for forensics

                success = false;

            }

            return success;    
        }    
    }

注意:我可以在邮递员工具中成功收到一个access_token。

1 个答案:

答案 0 :(得分:0)

最后,下面的代码非常适合ASP.NET MVC中的3条腿身份验证。

public class HomeController : Controller
    {
        //https://blackboard.jiveon.com/docs/DOC-3976-three-legged-oauth 

        public ActionResult Index()
        {
            // GET /learn/api/public/v1/oauth2/authorizationcode

            Guid stateId = Guid.NewGuid();

            string applicationKey = "Application key goes here";

            string redirectUrl = string.Format("Blackboard Learn URL goes here/learn/api/public/v1/oauth2/authorizationcode" +
                "?redirect_uri=https://localhost:44300/Home/OAuth2Response&response_type=code&client_id={0}&scope=read&state={1}",
                applicationKey, stateId);

            Response.Redirect(redirectUrl, true);

            return View();
        }


        public async Task<bool> OAuth2Response(string code = null, string state = null, string error = null, string error_description = null)
        {
            bool success = true;
            string json = string.Empty;
            string urlCommand = string.Format("/learn/api/public/v1/oauth2/token?code={0}&redirect_uri=https://localhost:44300/Home/OAuth2Response", code);

            try
            {
                using (HttpClient client = new HttpClient())
                {

                    var endpoint = new Uri("Blackboard Learn URL goes here" + urlCommand);


                    client.DefaultRequestHeaders.Accept.Clear();

                    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("client_id:client_secret")));                  

                    var postData = new List<KeyValuePair<string, string>>();
                    postData.Add(new KeyValuePair<string, string>("grant_type", "authorization_code")); 

                    HttpContent body = new FormUrlEncodedContent(postData);


                    using (HttpResponseMessage response = await client.PostAsync(endpoint, body))
                    {

                        if (response.IsSuccessStatusCode)
                        {
                            json = await response.Content.ReadAsStringAsync();

                            dynamic oauth2Result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);     

                            string access_token = oauth2Result.access_token;

                            string refresh_token = oauth2Result.refresh_token;           }
                        else
                        {
                            success = false;
                        }
                    }
                }
            }
            catch (Exception err)            {
                //hopefully we never end up here, log this exception for forensics      
                success = false;
            }
            return success;           
        }     
    }