Example #1
0
        /// <summary>
        /// 转跳到登录界面
        /// </summary>
        public void RedirectLogin()
        {
            string redirect  = $"{HttpContext.Request.Url.Scheme}/OAuth/AuthorizationCallBack";
            string returnUrl = HttpContext.Request.RawUrl;  //TODO:可以约定页面路径与api路径一致,多apiRoute
            string loginUrl  = OAuthHelper.GenerateLoginUrl(redirect, returnUrl);

            HttpContext.Response.Redirect(loginUrl);
        }
Example #2
0
        /// <summary>
        /// 回调
        /// </summary>
        /// <param name="code"></param>
        /// <param name="state"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public ActionResult AuthorizationCallBack(string code, string state, string returnUrl)
        {
            string cookieKey   = JwellConfig.GetAppSetting("accessToken");
            var    stateCookie = Request.Cookies["state"];

            //判断State是否一致
            if (stateCookie != null && stateCookie.Value.Equals(state))
            {
                //stateCookie.Expires = DateTime.Now.AddDays(-1); //内存Cookie
                //stateCookie.Path = JwellConfig.AppSettings("WebRootPath");
                System.Web.HttpContext.Current.Response.AppendCookie(stateCookie);

                var param = new NameValueCollection
                {
                    ["clientId"]     = JwellConfig.GetAppSetting("clientId"),
                    ["clientSecret"] = JwellConfig.GetAppSetting("clientSecret"),
                    ["redirectUrl"]  = JwellConfig.GetAppSetting("redirectUrl"),
                    ["code"]         = code,
                    ["grantType"]    = "authorizationCode"
                };
                //根据Code申请Token
                var token = GetToken(cookieKey, param);
                if (token != null)
                {
                    int expireTime = 0;
                    int.TryParse(JwellConfig.GetAppSetting("codeExpire"), out expireTime);
                    var cookie = new HttpCookie(cookieKey, token.Value <string>())
                    {
                        //cookie过期时间固定设置为12小时,与token过期时间一致
                        Expires = DateTime.Now.AddHours(expireTime)
                    };

                    var userInfo = GetUserInfo(token.Value <string>(), cookieKey);
                    Request.RequestContext.HttpContext.Session["userContext"] = userInfo;

                    System.Web.HttpContext.Current.Response.AppendCookie(cookie);
                    if (returnUrl.Contains("~")) //解决前端URl存在#的问题
                    {
                        returnUrl = returnUrl.Replace("~", "#");
                    }
                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToAction("Index", "register"));
                    }
                    return(Redirect(returnUrl));
                }
            }
            return(Redirect(OAuthHelper.GenerateLoginUrl(this.Url.Action("AuthorizationCallBack", "OAuth", null, Request.Url.Scheme),
                                                         $"http://{HttpContext.Request.Url.Authority}/register/index")));
        }
Example #3
0
        /// <summary>
        /// 回调
        /// </summary>
        /// <param name="code">此处code为employeeID</param>
        /// <param name="state"></param>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        public ActionResult AuthorizationCallBack(string code, string state, string returnUrl)
        {
            string cookieKey = JwellConfig.GetAppSetting("accessToken");


            if (JwellConfig.GetAppSetting("scope") == ApplicationConstant.BASEINFO)
            {
                var param = new NameValueCollection
                {
                    ["clientId"]     = JwellConfig.GetAppSetting("clientId"),
                    ["clientSecret"] = JwellConfig.GetAppSetting("clientSecret"),
                    ["code"]         = code,
                    ["grantType"]    = "authorizationCode"
                };
                //根据Code申请Token
                var token = GetToken(cookieKey, param);
                if (token != null)
                {
                    int expireTime = 0;
                    int.TryParse(JwellConfig.GetAppSetting("codeExpire"), out expireTime);
                    var cookie = new HttpCookie(cookieKey, token.Value <string>())
                    {
                        //cookie过期时间固定设置为12小时,与token过期时间一致
                        Expires = DateTime.Now.AddHours(expireTime)
                    };

                    var userInfo = GetUserInfo(token.Value <string>(), cookieKey);
                    Request.RequestContext.HttpContext.Session["userContext"] = userInfo;

                    System.Web.HttpContext.Current.Response.AppendCookie(cookie);
                    if (!string.IsNullOrWhiteSpace(returnUrl))
                    {
                        if (returnUrl.Contains("~")) //解决前端URl存在#的问题
                        {
                            returnUrl = returnUrl.Replace("~", "#");
                        }
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Home"));
                    }
                    return(Redirect(returnUrl));
                }
            }
            return(Redirect(OAuthHelper.GenerateLoginUrl(this.Url.Action("AuthorizationCallBack", "OAuth", null, Request.Url.Scheme),
                                                         $"http://{HttpContext.Request.Url.Authority}/Home/index")));
        }
        /// <summary>
        /// 进行页面验证,查看Token是否存在
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.ActionDescriptor.GetCustomAttributes(false).Any(g => g.GetType() == typeof(AllowAnonymousAttribute)))
            {
                base.OnAuthorization(filterContext);
                return;
            }
            var token = filterContext.HttpContext.Request.Cookies[JwellConfig.GetAppSetting("AccessToken")];

            if (token != null && !string.IsNullOrEmpty(token.Value))
            {
                return;
            }
            var returnUri = filterContext.HttpContext.Request.Url.ToString();
            var urlHelper = new UrlHelper(filterContext.RequestContext);

            filterContext.Result = new RedirectResult(OAuthHelper.GenerateLoginUrl(urlHelper.Action("AuthorizationCallBack", "OAuth", null, filterContext.HttpContext.Request.Url.Scheme), returnUri));
        }
Example #5
0
 public string LoginUrl(string returnUrl)
 {
     return(OAuthHelper.GenerateLoginUrl($"http://{Url.Request.Headers.Host}/AuthorizationCallBack/OAuth", returnUrl));
 }