예제 #1
0
        public async Task <(bool Succeed, string ErrorMessage, AuthResult Result)> Login(LoginView login)
        {
            var user = await Dapper.QueryFirstOrDefaultAsync(
                "select id,email,password,nickname from Users where email=@Email;",
                new { login.Email });

            if (user == null)
            {
                return(false, "Email does not exist!", null);
            }
            if (!BCryptor.Verify(login.Password, user.password))
            {
                return(false, "The password is incorrect!", null);
            }
            var claims = new[]
            {
                new Claim(ClaimTypes.Sid, user.id.ToString()),
                new Claim(ClaimTypes.Name, user.nickname),
                new Claim(ClaimTypes.NameIdentifier, login.Email),
                new Claim(ClaimTypes.Role, "user"),
            };
            var token = TokenBuilder.Build(claims, TimeSpan.FromDays(1));

            return(true, "", new AuthResult
            {
                Token = token.Token,
                Expire = token.Expires
            });
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("SHA1:" + Hash.SHA1("abc"));
            Console.WriteLine("SHA256:" + Hash.SHA256("abc"));
            Console.WriteLine("SHA384:" + Hash.SHA384("abc"));
            Console.WriteLine("SHA512:" + Hash.SHA512("abc"));
            Console.WriteLine("MD5:" + Hash.MD5("abc"));
            Console.WriteLine("HamcSHA1:" + Hash.HMACSHA1("abc", "123"));
            Console.WriteLine("HamcSHA256:" + Hash.HMACSHA256("abc", "123"));
            Console.WriteLine("HamcSHA384:" + Hash.HMACSHA384("abc", "123"));
            Console.WriteLine("HamcSHA512:" + Hash.HMACSHA512("abc", "123"));
            Console.WriteLine("HamcMD5:" + Hash.HMACMD5("abc", "123"));

            var aesKey = AES.GenerateKey();

            Console.WriteLine($"AES Key:{aesKey}");
            var aesEncrypt = AES.Encrypt("abc", aesKey);

            Console.WriteLine($"AES Encrypt:{aesEncrypt}");
            Console.WriteLine($"AES Decrypt:{AES.Decrypt(aesEncrypt, aesKey)}");

            var bCrypt = BCryptor.Encrypt("abc", BCryptor.GenerateSalt());

            Console.WriteLine($"BCrypt:{bCrypt}");
            Console.WriteLine($"BCrypt Verify:{BCryptor.Verify("abc", bCrypt)}");
            Console.ReadKey();
        }
예제 #3
0
        public async Task <(bool Succeed, string ErrorMessage)> Register(RegisterView userInfo)
        {
            if (await Dapper.ExecuteScalarAsync <int>("select count(1) from Users where email=@Email",
                                                      new { userInfo.Email }) > 0)
            {
                return(false, "Email address already exists");
            }
            var ok = await Dapper.ExecuteAsync(
                "insert into Users(email,password,nickname) values(@Email,@Password,@NickName);", new
            {
                userInfo.Email,
                Password = BCryptor.Encrypt(userInfo.Password),
                userInfo.NickName
            }) > 0;

            return(ok ? (true, "") : (false, "Registration failed"));
        }