Example #1
0
        public ActionResult Callback()
        {
            var code = Request.QueryString.Get("code");

            if (string.IsNullOrEmpty(code))//没有code表示授权失败
            {
                return(RedirectToAction("Failed", "OAuth"));
            }
            var     state              = Request.QueryString.Get("state");
            var     cache_status       = System.Web.HttpContext.Current.Cache.Get(state);
            var     redirect_url       = cache_status == null ? "/#/shop" : cache_status.ToString();//没有获取到state,就跳转到首页
            var     access_token_scope = "";
            double  expires_in         = 0;
            var     access_token       = "";
            var     openId             = "";
            var     token              = OAuth2API.GetAccessToken(code, WeixinConfig.AppID, WeixinConfig.AppSecret);
            dynamic userinfo;

            var refreshAccess_token = OAuth2API.RefreshAccess_token(token.refresh_token, WeixinConfig.AppID);

            access_token       = refreshAccess_token.access_token;//通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。
            openId             = refreshAccess_token.openid;
            access_token_scope = refreshAccess_token.scope;
            expires_in         = refreshAccess_token.expires_in;
            userinfo           = OAuth2API.GetUserInfo(access_token, openId);//snsapi_userinfo,可以用户在未关注公众号的情况下获取用户基本信息

            userinfo.Roles = "Weixin";
            //写入cookies
            FormsAuthHelper.AddFormsAuthCookie(openId, userinfo, 120);
            Thread.Sleep(500);//暂停半秒钟,以等待IOS设置Cookies的延迟
            LogWriter.Default.WriteInfo(string.Format("OAuth success: identity: {0} , name: {1} , redirect_rul:{2} , expires_in: {3}s ", openId, userinfo.nickname, redirect_url, expires_in));
            return(new RedirectResult(redirect_url, true));
        }
Example #2
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            if (string.IsNullOrWhiteSpace(model.UserName) || string.IsNullOrWhiteSpace(model.Password))
            {
                ModelState.AddModelError("", "请输入用户名和密码");
                return(View(model));
            }
            string msg = string.Empty;

            using (IT_Admin repository = new T_AdminRepository())
            {
                if (!repository.CheckUserAndPwd(model.UserName, model.Password))
                {
                    ModelState.AddModelError("", "用户名或密码错误");
                    return(View(model));
                }
            }

            model.Roles = "Admin";
            FormsAuthHelper.AddFormsAuthCookie(model.UserName, model, 0);//设置ticket票据的名称为用户的id,设置有效时间为60分钟

            return(Redirect(returnUrl ?? "~/Admin"));
        }