You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
|
|
|
|
namespace OAuth2Studyl.OAuthClient.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class DefaultController : Controller
|
|
|
|
|
{
|
|
|
|
|
// GET: Default
|
|
|
|
|
public ActionResult Index()
|
|
|
|
|
{
|
|
|
|
|
ViewData["OAuthDomain"] = GetOAuthServerDomain();
|
|
|
|
|
ViewData["RedictDomain"] = GetRedictUrlDomain();
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Code(string code="", string state="")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//无Code参数,表示用户拒绝授权
|
|
|
|
|
if (string.IsNullOrEmpty(code))
|
|
|
|
|
{
|
|
|
|
|
return View("CodeReject");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//后台请求AccessToken
|
|
|
|
|
var questUrl = GetOAuthServerDomain() + "api/OAuth2/GetAccessToken?grant_type=accesstoken&code=" + code + "&redirect_uri=" + GetRedictUrlDomain() + "&client_id=1";
|
|
|
|
|
var accessTokenResult = GetAccessTokenResult(questUrl);
|
|
|
|
|
|
|
|
|
|
ViewData["AccessTokenInfo"] = accessTokenResult;
|
|
|
|
|
return View();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAccessTokenResult(string url)
|
|
|
|
|
{
|
|
|
|
|
HttpClient client = new HttpClient();
|
|
|
|
|
|
|
|
|
|
var result = client.GetStringAsync(url).Result;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetOAuthServerDomain()
|
|
|
|
|
{
|
|
|
|
|
var url = System.Configuration.ConfigurationManager.AppSettings["OAuthDomain"] ?? "http://localhoast:2000/";
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetRedictUrlDomain()
|
|
|
|
|
{
|
|
|
|
|
var domain = string.Format("{0}://{1}{2}/", Request.Url.Scheme, Request.Url.Host, Request.Url.Port == 80 ? "" : ":" + Request.Url.Port);
|
|
|
|
|
domain = Request.UrlDomain();
|
|
|
|
|
|
|
|
|
|
//return Request.Url.ToString().Split('?')[0];
|
|
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|