Beispiel #1
0
        public ActionResult Signin(SigninInfo data)
        {
            string username = "******";
            string password = "******";


            if (this.ModelState.IsValid)
            {
                if (username.Equals(data.Username) && password.Equals(data.Password))
                {
                    ClaimsIdentity claimsIdentity =
                        new ClaimsIdentity("ApplicationCookie");
                    claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, data.Username));
                    claimsIdentity.AddClaim(new Claim("PassportUrl", Url.Content("~/images/profile.png")));


                    var ctxt = this.Request.GetOwinContext();
                    ctxt.Authentication.SignIn(claimsIdentity);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    this.ModelState.AddModelError("", "Username or password is invalid");
                }
            }
            return(View(data));
        }
        public async Task <ActionResult> Signin(SigninInfo account, String returnUrl)
        {
            if (ModelState.IsValid)
            {
                User user = await UserManager.FindAsync(account.Name, account.Password);

                if (user == null)
                {
                    ModelState.AddModelError("", "Invalid name or password");
                }
                else
                {
                    ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user,
                                                                                    DefaultAuthenticationTypes.ApplicationCookie);

                    AuthManager.SignOut();
                    AuthManager.SignIn(new AuthenticationProperties {
                        IsPersistent = true
                    }, identity);

                    if (String.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToAction("List", "Book"));
                    }
                    else
                    {
                        // if a return url exist, user will be sent to the page
                        return(Redirect(returnUrl));
                    }
                }
            }

            return(View(account));
        }
Beispiel #3
0
        public IActionResult Signin([FromBody] SigninInfo signinInfo)
        {
            var account = Ledger.Accounts.SingleOrDefault(a => a.Username == signinInfo.Username.ToLower());

            if (account == null)
            {
                return(Unauthorized());
            }

            if (!VerifyPasswordHash(signinInfo.Password, account.PasswordHash, account.PasswordSalt))
            {
                return(Unauthorized());
            }

            var claims = new[]
            {
                new Claim(ClaimTypes.Name, account.Username)
            };

            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:SecretKey").Value));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = credentials
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var token        = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new { token = tokenHandler.WriteToken(token) }));
        }