Exemple #1
0
        public async Task <IActionResult> UserLogin([FromBody] UserLoginDto loginDto)
        {
            var session = await GetSessionKey(loginDto.Code);

            if (session.openid == null)
            {
                return(BadRequest());
            }
            var user = await userRepository.GetUserByOpenIDAsync(session.openid);

            if (user == null)
            {
                cache.Set("Session_" + session.openid, session.session_key, CacheExpireSpan);
                cache.Set("UnionID_" + session.openid, session.unionid, CacheExpireSpan);
                return(Unauthorized(session.openid));
            }
            user.SessionKey = session.session_key;
            await userRepository.SaveAsync();

            var token        = Jwt.GenerateJWT(configuration, user.UserID.ToString(), "User");
            var jsonToReturn = JsonSerializer.Serialize(
                new
            {
                accessToken = token,
                userid      = user.UserID
            });

            return(Ok(jsonToReturn));
        }
Exemple #2
0
        public async Task <IActionResult> CreateUser(UserAddDto user)
        {
            var    sessionKey = "Session_" + user.OpenID;
            var    unionKey   = "UnionID_" + user.OpenID;
            string session    = string.Empty;

            if (!cache.TryGetValue(sessionKey, out session))
            {
                return(NotFound(nameof(user.OpenID)));
            }
            string unionid = cache.Get <string>(unionKey);

            cache.Remove(sessionKey);
            //var decryptedData = WXEncrypt.AESDecrypt(user.EncryptedData, session_key, user.Iv);

            User newUser = new User
            {
                OpenID     = user.OpenID,
                UnionID    = unionid,
                SessionKey = session,
                NickName   = user.UserInfo.NickName,
                Gender     = user.UserInfo.Gender,
                Language   = user.UserInfo.Language,
                City       = user.UserInfo.City,
                Province   = user.UserInfo.Province,
                Country    = user.UserInfo.Country,
                AvatarUrl  = user.UserInfo.AvatarUrl
            };

            userRepository.AddUser(newUser);
            await userRepository.SaveAsync();

            var token        = Jwt.GenerateJWT(configuration, newUser.UserID.ToString(), "User");
            var jsonToReturn = JsonSerializer.Serialize(
                new
            {
                accessToken = token,
                userid      = newUser.UserID
            });

            return(Ok(jsonToReturn));
        }
Exemple #3
0
        public IActionResult AdminLogin([FromBody] AdminLoginDto loginDto)
        {
            //User Authentication
            if (string.IsNullOrWhiteSpace(loginDto.Username) || string.IsNullOrWhiteSpace(loginDto.Password))
            {
                return(BadRequest("Email or Password can not be empty"));
            }

            //因为Admin用户只有一个,也不提供注册功能,想想还是不用存数据库了
            var username = configuration["AdminUser:UserName"];
            var password = configuration["AdminUser:Password"];

            if (!username.Equals(loginDto.Username) || !password.Equals(loginDto.Password))
            {
                return(Unauthorized());
            }

            //比对通过即可发Token。
            var token = Jwt.GenerateJWT(configuration, username, "Admin");

            return(Ok(token));
        }