Exemplo n.º 1
0
        public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            Organisation       org = Organisations.TryLogin(context.UserName, context.Password);
            RegisterManagement reg = null;
            int tryId;

            if (Int32.TryParse(context.UserName, out tryId) &&
                RegistersManagement.GetRegisters().Exists(r => r.ID == Int32.Parse(context.UserName) && r.AssignedTo != null))
            {
                reg = RegistersManagement.GetRegisters().FirstOrDefault(r => r.ID == Int32.Parse(context.UserName));
            }

            // try to log in
            if (org != null)
            {
                var id = new ClaimsIdentity(context.Options.AuthenticationType);
                id.AddClaim(new Claim("username", context.UserName));
                id.AddClaim(new Claim("connectionString", org.DatabaseConnectionString));
                id.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                id.AddClaim(new Claim(ClaimTypes.Role, "OrganisationManager"));

                context.Validated(id);
            }
            else if (reg != null && reg.RemotePassword == context.Password)
            {
                var id = new ClaimsIdentity(context.Options.AuthenticationType);
                id.AddClaim(new Claim("username", context.UserName));
                id.AddClaim(new Claim("connectionString", reg.AssignedTo.DatabaseConnectionString));
                id.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
                id.AddClaim(new Claim(ClaimTypes.Role, "Register"));

                context.Validated(id);
            }
            else
            {
                context.Rejected();
            }

            return(Task.FromResult(0));
        }
Exemplo n.º 2
0
        public object ChangePassword(string oldPassword, string newPassword)
        {
            if (oldPassword == null || newPassword == null || oldPassword == "" || newPassword == "")
            {
                return("false");
            }

            if (Organisations.TryLogin(User.Identity.Name, oldPassword) == null)
            {
                return("false");
            }

            var org = Organisations.GetByUser(User.Identity.Name);

            if (org == null)
            {
                return("false");
            }

            Organisations.ChangePassword(org.ID, newPassword);

            return("true");
        }