protected override async Task<IPrincipal> AuthenticateAsync(string userName, string password, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            //if (userName != "testuser" || password != "Pass1word")
            //{
            //    // No user with userName/password exists.
            //    return null;
            //}

            //// Create a ClaimsIdentity with all the claims for this user.
            //Claim nameClaim = new Claim(ClaimTypes.Name, userName);
            //List<Claim> claims = new List<Claim> { nameClaim };

            //// important to set the identity this way, otherwise IsAuthenticated will be false
            //// see: http://leastprivilege.com/2012/09/24/claimsidentity-isauthenticated-and-authenticationtype-in-net-4-5/
            ////ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationTypes.Basic);
            //ClaimsIdentity identity = new ClaimsIdentity(claims, "Basic");

            //var principal = new ClaimsPrincipal(identity);
            //return principal;

            Usuario newModel = new Usuario();
            if (Usuario.ExisteUsuario(userName))
            {
                if (!Usuario.EsLoginValido(userName, password))
                {
                    // No user with userName/password exists.
                    return null;

                }

                // Create a ClaimsIdentity with all the claims for this user.
                Claim nameClaim = new Claim(ClaimTypes.Name, userName);
                List<Claim> claims = new List<Claim> { nameClaim };

                // important to set the identity this way, otherwise IsAuthenticated will be false
                // see: http://leastprivilege.com/2012/09/24/claimsidentity-isauthenticated-and-authenticationtype-in-net-4-5/
                //ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationTypes.Basic);
                ClaimsIdentity identity = new ClaimsIdentity(claims, "Basic");

                var principal = new ClaimsPrincipal(identity);
                return principal;

            }

            else return null;

        }
Пример #2
0
        public ActionResult Login(LoginViewModel newUser, string returnUrl)
        {
            //Login Code
            try
            {
                if (ModelState.IsValid)
                {
                    Usuario newModel = new Usuario();
                    if (Usuario.EsLoginValido(newUser.cUserName, newUser.cPassword))
                    {
                        FormsAuthentication.SetAuthCookie(newUser.cUserName, false);
                        Session["Usuario"] = newUser.cUserName;

                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            //return RedirectToAction("Welcome", "Home");
                            return RedirectToAction("Index", "Topologia");
                        }
                        //return RedirectToAction("Index", "Topologia");

                    }
                    else
                    {
                        ModelState.AddModelError("", "El nombre de usuario o contraseña están incorrectos.");
                    }
                }
            }
            catch
            {
                Session["Usuario"] = null;
                return View(newUser);
            }

            return View(newUser);
        }
Пример #3
0
        public ActionResult Registrar(RegistroViewModel newUser)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Usuario newModel = new Usuario();
                    if (!Usuario.ExisteUsuario(newUser.cUserName))
                    {
                        newModel.InsertarUsuario(newUser);
                        FormsAuthentication.SetAuthCookie(newUser.cUserName, false);
                        Session["Usuario"] = newUser.cUserName;
                        return RedirectToAction("Welcome", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "El nombre de usuario ya existe.");
                    }
                }
            }
            catch
            {
                Session["Usuario"] = null;
                return View(newUser);
            }

            return View(newUser);
        }