public GenericObjectResponse <UserResponse> GetUserData([FromQuery] string username) { UserResponse response = null; long? queryingUserId = AuthenticationService.IsAuthorized(Request, UserRole.Coach, UserRole.RoomOwner); if (queryingUserId == null) { Response.StatusCode = 401; return(new GenericObjectResponse <UserResponse>($"Unauthorized request.")); } else { UserQueryService userQueryService = new UserQueryService(); if (username == null) { response = userQueryService.GetUserById(queryingUserId.Value); } else { response = userQueryService.FindUser(username, queryingUserId.Value); } } if (response == null) { Response.StatusCode = 404; return(new GenericObjectResponse <UserResponse>($"Could not find user {username}.")); } return(new GenericObjectResponse <UserResponse>(response)); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { await Task.Factory.StartNew(() => context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" })); // 对用户名、密码进行数据校验 using (UserQueryService _userQueryServices = new UserQueryService()) { var userinfo = _userQueryServices.FindUser(context.UserName); if (userinfo == null) { context.SetError("invalid_grant", "The user name is incorrect."); return; } if (!PasswordHash.ValidatePassword(context.Password, userinfo.Password)) { context.SetError("invalid_grant", "The user password is incorrect."); return; } } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); context.Validated(identity); }
/// <summary> /// 获取用户信息 1缓存》2数据库 /// </summary> /// <param name="userId"></param> /// <returns></returns> public UserViewModel GetUserInfo(string userId) { userId.CheckNotNullOrEmpty(nameof(userId)); var userInfo = _cache.Get(CacheKeySupplier.UserModelCacheKey(userId)) as UserViewModel; if (userInfo == null) { userInfo = _userQueryService.FindUser(userId.ToGuid()).ToUserModel(); _cache.Add(CacheKeySupplier.UserModelCacheKey(userId), userInfo); } return(userInfo); }
public BaseApiResponse Login(LoginRequest request) { request.CheckNotNull(nameof(request)); if (!request.Mobile.IsMobileNumber()) {//是否手机号 return(new BaseApiResponse { Code = 400, Message = "手机号格式不正确" }); } var userinfo = _userQueryService.FindUser(request.Mobile); //验证用户 if (userinfo == null) { return(new BaseApiResponse { Code = 400, Message = "没找到该账号" }); } //验证密码 if (!PasswordHash.ValidatePassword(request.Password, userinfo.Password)) { return(new BaseApiResponse { Code = 400, Message = "登录密码错误" }); } //设置cookie 和缓存 _apiSession.SetAuthCookie(HttpContext.Current.Response, userinfo.Id.ToString()); _apiSession.SetUserInfo(userinfo.Id.ToString(), userinfo.ToUserModel()); //获取钱包信息 var walletinfo = _walletQueryService.Info(userinfo.WalletId); if (walletinfo == null) { return(new BaseApiResponse { Code = 400, Message = "获取钱包信息失败" }); } _apiSession.SetWalletInfo(walletinfo.Id.ToString(), walletinfo.ToWalletModel()); //购物车信息 var cart = _cartQueryService.Info(userinfo.CartId); if (cart == null) { return(new BaseApiResponse { Code = 400, Message = "获取购物车信息失败" }); } //店铺信息 var storeId = ""; var storeinfo = _storeQueryService.InfoByUserId(userinfo.Id); if (storeinfo != null) { storeId = storeinfo.Id.ToString(); } return(new LoginResponse { UserInfo = new UserInfo { Id = userinfo.Id, ParentId = userinfo.ParentId, NickName = userinfo.NickName, Portrait = userinfo.Portrait.ToOssStyleUrl(OssImageStyles.UserPortrait.ToDescription()), Mobile = userinfo.Mobile, Gender = userinfo.Gender, Region = userinfo.Region, Role = userinfo.Role.ToDescription(), StoreId = storeId, CartId = userinfo.CartId.ToString(), CartGoodsCount = cart.GoodsCount, Token = userinfo.Id.ToString() }, WalletInfo = new WalletInfo { Id = walletinfo.Id, AccessCode = walletinfo.AccessCode, Cash = walletinfo.Cash, Benevolence = walletinfo.Benevolence, Earnings = walletinfo.Earnings, YesterdayEarnings = walletinfo.YesterdayEarnings } }); }