Exemple #1
0
        public SignUpResult SignUp(string name, string credentialTypeCode, string identifier, string secret)
        {
            Users user = new Users();

            user.Name    = name;
            user.Created = DateTime.Now.ToShortDateString();
            this._context.Users.Add(user);
            this._context.SaveChanges();

            CredentialTypes credentialType = this._context.CredentialTypes.FirstOrDefault(ct => string.Equals(ct.Code, credentialTypeCode, StringComparison.OrdinalIgnoreCase));

            if (credentialType == null)
            {
                return(new SignUpResult(success: false, error: SignUpResultError.CredentialTypeNotFound));
            }

            Credentials credential = new Credentials();

            credential.UserId           = user.Id;
            credential.CredentialTypeId = credentialType.Id;
            credential.Identifier       = identifier;

            if (!string.IsNullOrEmpty(secret))
            {
                byte[] salt = Pbkdf2Hasher.GenerateRandomSalt();
                string hash = Pbkdf2Hasher.ComputeHash(secret, salt);

                credential.Secret = hash;
                credential.Extra  = Convert.ToBase64String(salt);
            }

            this._context.Credentials.Add(credential);
            this._context.SaveChanges();
            return(new SignUpResult(user: user, success: true));
        }
Exemple #2
0
        public ChangeSecretResult ChangeSecret(string credentialTypeCode, string identifier, string secret)
        {
            CredentialTypes credentialType = this._context.CredentialTypes.FirstOrDefault(ct => string.Equals(ct.Code, credentialTypeCode, StringComparison.OrdinalIgnoreCase));

            if (credentialType == null)
            {
                return(new ChangeSecretResult(success: false, error: ChangeSecretResultError.CredentialTypeNotFound));
            }

            Credentials credential = this._context.Credentials.FirstOrDefault(c => c.CredentialTypeId == credentialType.Id && c.Identifier == identifier);

            if (credential == null)
            {
                return(new ChangeSecretResult(success: false, error: ChangeSecretResultError.CredentialNotFound));
            }

            byte[] salt = Pbkdf2Hasher.GenerateRandomSalt();
            string hash = Pbkdf2Hasher.ComputeHash(secret, salt);

            credential.Secret = hash;
            credential.Extra  = Convert.ToBase64String(salt);
            this._context.Credentials.Update(credential);
            this._context.SaveChanges();
            return(new ChangeSecretResult(success: true));
        }