alun-chen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

上一篇文章中,我们介绍了怎么创建自己的服务器,现在我们开始写个client端,来测试。

 

我们创建一个MVC项目,叫TestOAuthClient

1. 代码开始

1)第一步,我们创建一个MainController,在Index方法里面写我们的逻辑。

2)首先获取code,如果没有code,则证明是第一步请求。

3)第一步请求,附上client_id、response_type、redirect_uri、scope、state参数。

这里我们假如服务端的第一步请求认证的地址为:http://localhost:65006/OAuth2Server/Authorize

     client_id是请求端在服务端申请的id;

     response_type为code;

     redirect_uri是告诉服务端,获取code之后返回的地址是什么;

     scope自定义;

     state自定义。

4)跳转到验证服务器。

5)验证服务器重定向会我们的请求端后(code不为空),请求获取token。

获取token需要传送返回的code、grant_type=authorization_code、client_id、client_secret

6)通过服务器返回的token,请求服务端获取用户信息。

代码就几行,如下:

        public ActionResult Index()
        {
            string code = Request["code"] ?? "";

            if (string.IsNullOrEmpty(code))
            {
                //第一步,请求获取code(请求OAuth服务器)
                string client_id = "testclientid";
                string response_type = "code";
                string redirect_uri = HttpUtility.UrlEncode("http://localhost:61481/Main/Index");
                string scope = "";
                string state = "";
                string url = string.Format
                    ("http://localhost:65006/OAuth2Server/Authorize?client_id={0}&response_type={1}&redirect_uri={2}&scope={3}&state={4}",
                   client_id, response_type, redirect_uri, scope, state);
                Response.Redirect(url);
                return null;
            }
            else
            {
                //第二步,获取code之后请求获取token(请求OAuth服务器)
                RestClient clientToken = new RestClient("http://localhost:65006/OAuth2Server/GetToken");
                IRestRequest requestToken = new RestRequest();
                requestToken.AddParameter("code", code);
                requestToken.AddParameter("grant_type", "authorization_code");
                requestToken.AddParameter("client_id", "testclientid");
                requestToken.AddParameter("client_secret", "testclientsecret");
                IRestResponse responseToken = clientToken.Execute(requestToken);
                string access_token = responseToken.Content.Replace("\"", "");

                //第三部,获取token之后,获取user信息(请求OAuth服务器)
                RestClient clientUserInfo = new RestClient("http://localhost:65006/OAuth2Server/UserInfo");
                IRestRequest requestUserInfo = new RestRequest();
                requestUserInfo.AddParameter("oauth_token", access_token);
                IRestResponse responseUserInfo = clientUserInfo.Execute(requestUserInfo);
                string userInfoContent = responseUserInfo.Content;
                //返回获取到的用户信息
                return this.Json("userInfoContent=" + userInfoContent, JsonRequestBehavior.AllowGet);
            }
        }

 

源代码如下: https://github.com/cjt321/TestOAuthClient/

 

2. 开始调试

1)请求端(TestOAuthClient)的地址为:http://localhost:61481/Main/Index

2)在浏览器上输入上面地址,会重定向到用户是否允许授权的页面。(此页面是服务端的页面)

image

当我们输入正确的用户名&密码之后,会发现,再请求端能获取到用户的信息。

到此,测试结束。

 

可以关注本人的公众号,多年经验的原创文章共享给大家。

posted on 2017-06-07 16:36  alun-chen  阅读(5359)  评论(2编辑  收藏  举报