Beispiel #1
0
        public ActionResult Info()
        {
            var userId = GetUserID();

            if (userId == 0)
            {
                return(Json(new { success = false, error = "用户尚未登录." }));
            }
            var userInfo = userDataDapper.FindByID(userId);

            return(Json(new { success = true, data = userInfo }));
        }
 public ActionResult Auth(string code, string state)
 {
     if (!string.IsNullOrEmpty(code))
     {
         try
         {
             var accessToken = authClient.GetAccessToken(code).Result;
             var qqUser      = authClient.GetUserInfo(accessToken).Result;
             var userInfo    = userDataDapper.FindByID(GetUserID());
             //已登录 = 绑定此QQ
             if (userInfo != null)
             {
                 userInfo.QQOpenUID = qqUser.Id;
                 userDataDapper.UpdateUser(userInfo, new List <string>()
                 {
                     "QQOpenUID"
                 });
                 return(View(new ViewResult()
                 {
                     success = true, message = "绑定成功!"
                 }));
             }
             //未登录,通过此ID获取用户
             userInfo = userDataDapper.FindUserByQQOpenUID(qqUser.Id);
             if (userInfo == null)
             {
                 //新增用户
                 userDataDapper.InsertUserForQQAuth(new UserInfo()
                 {
                     UserName = qqUser.Name, QQOpenUID = qqUser.Id
                 });
                 userInfo = userDataDapper.FindUserByQQOpenUID(qqUser.Id);
             }
             var user = new ClaimsPrincipal(new ClaimsIdentity(new[]
             {
                 new Claim(ClaimTypes.Name, userInfo.UserName),
                 new Claim(ClaimTypes.Email, userInfo.Email),
                 new Claim(ClaimTypes.NameIdentifier, userInfo.ID.ToString())
             }, CookieAuthenticationDefaults.AuthenticationScheme));
             HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user,
                                     new AuthenticationProperties
             {
                 IsPersistent = true,
                 ExpiresUtc   = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间
             }).Wait();
             return(View(new ViewResult()
             {
                 success = true, message = "登录成功!"
             }));
         }
         catch (Exception ex)
         {
             return(View(new ViewResult()
             {
                 success = false, error = ex.ToString()
             }));
         }
     }
     return(View(new ViewResult()
     {
         success = false, error = "无效的auth code"
     }));
 }