Exemplo n.º 1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            using (var db = new AutenticacionEntities())
            {
                if (db != null)
                {
                    var empl = db.Employees.ToList();
                    var user = db.Users.ToList();
                    if (user != null)
                    {
                        if (!string.IsNullOrEmpty(user.Where(u => u.UserName == context.UserName && u.Password == context.Password).FirstOrDefault().Name))
                        {
                            identity.AddClaim(new Claim("Age", "16"));

                            var props = new AuthenticationProperties(new Dictionary <string, string>
                            {
                                {
                                    "userdisplayname", context.UserName
                                },
                                {
                                    "role", "admin"
                                }
                            });

                            var ticket = new AuthenticationTicket(identity, props);
                            context.Validated(ticket);
                        }
                        else
                        {
                            context.SetError("invalid_grant", "Usuario o contraseña no válidos");
                            context.Rejected();
                        }
                    }
                }
                else
                {
                    context.SetError("invalid_grant", "Usuario o contraseña no válidos");
                    context.Rejected();
                }
                return;
            }
        }
Exemplo n.º 2
0
        public override bool IsUserInRole(string username, string roleName)
        {
            var key = ConfigurationManager.AppSettings["ClaveCifrado"];
            var cif = SeguridadUtility.Encrypt(username, key);

            using (var db = new AutenticacionEntities())
            {
                try
                {
                    var us = db.Usuario.First(o => o.Login.Equals(cif));
                    return us.Rol.Nombre.Equals(roleName);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }
Exemplo n.º 3
0
        public override string[] GetRolesForUser(string username)
        {
            var key = ConfigurationManager.AppSettings["ClaveCifrado"];
            var cif = SeguridadUtility.Encrypt(username, key);

            using (var db = new AutenticacionEntities())
            {
                try
                {
                    var us = db.Usuario.First(o => o.Login.Equals(cif));
                    return new[] { us.Rol.Nombre };
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return null;
                }
            }
        }
 public override bool ValidateUser(string username, string password)
 {
     using (var db = new AutenticacionEntities())
     {
         var pss = SeguridadUtility.GetSha1(password);
         return db.Usuario.Any(o => o.Login.Equals(username) && o.Password.Equals(pss));
     }
 }
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            using (var db = new AutenticacionEntities())
            {
                var user = db.Usuario.FirstOrDefault(o => o.Login.Equals(username));

                if (user == null)
                    return null;

                return new UsuarioMembership(user);
            }
        }