public MyResult <object> GetUserAuth(string name, string password) { MyResult result = new MyResult(); if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password)) { return(result.SetError("用户名密码不能为空")); } string auth_sql = $"select au.id,au.username,au.password,au.role_id roleId,ifnull(ar.role_name,'') roleName from admin_users au left join admin_roles ar on au.role_id=ar.id where au.username='******' and au.password='******'"; var userInfo = dbConnection.QuerySingleOrDefault(auth_sql); if (userInfo == null) { return(result.SetStatus(ErrorCode.ErrorUserNameOrPass, "用户名密码错误")); } var roleId = userInfo.roleId; string action_sql = $"select aa.action_name actionName,aa.code from admin_role_action ara left join admin_actions aa on ara.action_id=aa.id and aa.enable=1 where ara.role_id={roleId}"; var action = dbConnection.Query(action_sql); TokenModel tokenModel = new TokenModel(); tokenModel.Id = userInfo.id; tokenModel.Mobile = ""; tokenModel.Code = ""; tokenModel.Source = domain.enums.SourceType.Web; result.Data = new { token = DataProtectionUtil.Protect(tokenModel.GetJson()), userData = new { userInfo = userInfo, action = action } }; return(result); }
public IActionResult ValidateCode() { ValidateCode _vierificationCodeServices = new ValidateCode(); string code = ""; System.IO.MemoryStream ms = _vierificationCodeServices.Create(out code); CookieUtil.AppendCookie(Constants.WEBSITE_VERIFICATION_CODE, DataProtectionUtil.Protect(code)); return(File(ms.ToArray(), @"image/png")); }
public MyResult <object> Login(WxLoginDto model) { MyResult result = new MyResult(); if (string.IsNullOrEmpty(model.Code)) { return(result.SetStatus(ErrorCode.InvalidData, "code 无效")); } var code2SessionUrl = $"https://api.weixin.qq.com/sns/jscode2session?appid={Constants.WxAppId}&secret={Constants.WxSecret}&js_code={model.Code}&grant_type=authorization_code"; var rep = HttpUtil.GetString(code2SessionUrl); var repObj = rep.GetModel <Code2SessionRep>(); var openid = repObj.OpenId; var user = base.First <User>(predicate => predicate.OpenId == openid); if (user == null) { return(result.SetStatus(ErrorCode.NotFound, "用户未注册")); } user.SessionKey = repObj.Session_Key; TokenModel tokenModel = new TokenModel(); tokenModel.Id = (int)user.Id; tokenModel.Mobile = user.PhoneNum; tokenModel.Code = repObj.OpenId; tokenModel.Source = domain.enums.SourceType.WeChat; var tokenStr = tokenModel.GetJson(); var enToken = DataProtectionUtil.Protect(tokenStr); result.Data = new { token = enToken, uid = (int)user.Id }; user.Token = enToken; base.Update(user, true); return(result); }
public void Login(string scheme, Action <CookieOptions> options = null) { CookieUtil.AppendCookie(scheme, DataProtectionUtil.Protect(JsonConvert.SerializeObject(this)), true, options); }