Esempio n. 1
0
        public void TestEncrypt()
        {
            string val   = "bobo.huang";
            string enVal = AESCoding.Encrypt(val);
            string deVal = AESCoding.Decrypt(enVal);

            Assert.Equal(val, deVal);
        }
Esempio n. 2
0
        public IActionResult Login(string username, string password)
        {
            Random random = new Random();

            if (this.userService.Auth(username, password))
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                var claims   = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, username),
                    new Claim(ClaimTypes.Role, "Member")
                };
                identity.AddClaims(claims);

                string    signature = username;
                TokenInfo token     = new TokenInfo()
                {
                    UserName    = username,
                    IP          = HttpContext.Request.Host.Host,
                    Expiry      = DateTime.Now.AddHours(1),
                    CreatedDate = DateTime.Now
                };
                TokenModel tm = new TokenModel()
                {
                    UserName      = username,
                    ApplicationId = "BOBO",
                    Nonce         = random.Next(100000, 999999).ToString(),
                    Expiry        = Math.Ceiling((token.Expiry.Value.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds).ToString()
                };
                tm.Token    = AESCoding.Encrypt($"{tm.UserName}-{tm.ApplicationId}-{tm.Expiry}-{tm.Nonce}");
                token.Token = tm.Token;
                this.tokenInfoService.SaveToken(token);
                return(Ok(tm));
            }
            else
            {
                return(Ok(new UserInfo()));
            }
        }