public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (var repo = new LoginRepository())
            {
                var user = await repo.GetByEmail(context.UserName);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name does not exist.");
                    return;
                }
                if (user.PasswordHash == null)
                {
                    PasswordManager.SetUserPassword(user, context.Password);
                    await repo.Commit();
                }
                else
                {
                    bool valid = await PasswordManager.ValidatePassword(context.UserName, context.Password);

                    if (!valid)
                    {
                        context.SetError("invalid_grant", "The username and password combination is wrong.");
                        return;
                    }
                }

                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, user.Id.ToString()));
                context.Validated(identity);
            }
        }
Esempio n. 2
0
        public async Task Put(LoginDTO item)
        {
            using (var repo = new LoginRepository())
            {
                Login existing = await repo.GetById(item.Id);

                if (existing == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                item.Save(existing);
                await repo.Commit();
            }
        }
Esempio n. 3
0
        public async Task <int> Post(LoginDTO item)
        {
            using (var repo = new LoginRepository())
            {
                Login login = new Login();
                item.Save(login);

                await repo.Create(login);

                await repo.Commit();

                return(login.Id);
            }
        }
Esempio n. 4
0
        public async Task Delete(LoginDTO item)
        {
            using (var repo = new LoginRepository())
            {
                Login existing = await repo.GetById(item.Id);

                if (existing == null)
                {
                    return;
                }
                await repo.Delete(existing);

                await repo.Commit();
            }
        }