예제 #1
0
        public async Task <AuthUserDto> RegisterUser(AuthUserDto user)
        {
            var userrep = await _context.AuthUser.FirstOrDefaultAsync(x => x.UserName == user.UserName);

            string[] roleSplit = user.RoleName.Split(new char[] { ',' });

            foreach (string roleVal in roleSplit)
            {
                if (_context.UserRole != null)
                {
                    //cannit use string.compare
                    var role = await _context.UserRole.FirstOrDefaultAsync(
                        x => !string.IsNullOrWhiteSpace(roleVal) &&
                        !string.IsNullOrWhiteSpace(x.RoleName) &&
                        roleVal.ToLower() == x.RoleName.ToLower());

                    if (role == null)
                    {
                        role          = new Role();
                        role.RoleName = user.RoleName;
                        await _context.UserRole.AddAsync(role);

                        await _context.SaveChangesAsync();
                    }
                }
            }

            if (userrep == null)
            {
                userrep = new AuthUser();
            }

            userrep.UserName  = user.UserName;
            userrep.FirstName = user.FirstName;
            userrep.LastName  = user.LastName;
            userrep.Email     = user.Email;
            userrep.Address   = user.Address;
            userrep.UserandRole.Clear();


            userrep.UserandRole = new List <UserandRole>();

            foreach (string roleVal in roleSplit)
            {
                UserandRole userRole = new UserandRole();

                /*var role = await _context.UserRole.FirstOrDefaultAsync(
                 *  x => String.Equals(x.RoleName, roleVal,
                 *  StringComparison.OrdinalIgnoreCase));*/
                var role = await _context.UserRole.FirstOrDefaultAsync(
                    x => !string.IsNullOrWhiteSpace(roleVal) &&
                    !string.IsNullOrWhiteSpace(x.RoleName) &&
                    roleVal.ToLower() == x.RoleName.ToLower());

                userRole.UserRole   = role;
                userRole.RoleId     = role.RoleId;
                userRole.AuthUser   = userrep;
                userRole.AuthUserId = userrep.AuthUserId;
                userrep.UserandRole.Add(userRole);
            }

            byte[] passwordHash, passwordSalt;
            CreatePasswordHash(user.Password, out passwordHash, out passwordSalt);
            userrep.PasswordHash = passwordHash;
            userrep.PasswordSalt = passwordSalt;
            await _context.AuthUser.AddAsync(userrep);

            await _context.SaveChangesAsync();

            return(user);
        }
        public async Task <ClientCredentialDto> Register(ClientCredentialDto credential)
        {
            //The following code works with SQLLite but entity framework has issues with SQL Server
            var clientCred = await _context.ClientCredential.FirstOrDefaultAsync(x => x.ClientID == credential.ClientID);

            if (clientCred == null)
            {
                ClientCredentials cred = new ClientCredentials();
                cred.AbsoluteRefreshTokenLifetime = credential.AbsoluteRefreshTokenLifetime;
                cred.AccessTokenLifetime          = credential.AccessTokenLifetime;
                cred.AllowedGrantTypes            = credential.AllowedGrantTypes;

                //if(credential.AllowOfflineAccess == null)
                //    cred.AllowOfflineAccess = 0;
                //else
                //    cred.AllowOfflineAccess = credential.AllowOfflineAccess;

                cred.AllowOfflineAccess           = credential.AllowOfflineAccess;
                cred.ClientID                     = credential.ClientID;
                cred.RefreshTokenExpiration       = credential.RefreshTokenExpiration;
                cred.RefreshTokenUsage            = credential.RefreshTokenUsage;
                cred.AbsoluteRefreshTokenLifetime = credential.AbsoluteRefreshTokenLifetime;

                //byte[] passwordHash, passwordSalt;
                //AuthUserRepository.CreatePasswordHash(credential.ClientSecret, out passwordHash, out passwordSalt);
                //cred.ClientSecretHash = passwordHash;
                //cred.ClientSecretSalt = passwordSalt;
                cred.ClientSecretHash = Helper.EncryptDecrypt.EncryptString(credential.ClientSecret);
                _context.Database.SetCommandTimeout(30000);
                await _context.ClientCredential.AddAsync(cred);

                await _context.SaveChangesAsync();

                credential.Id = cred.Id;
                return(credential);
            }
            else
            {
                return(null);
            }



            /*using (Microsoft.Data.SqlClient.SqlConnection cn = new Microsoft.Data.SqlClient.SqlConnection(_context.Database.GetDbConnection().ConnectionString))
             * {
             *  await cn.OpenAsync();
             *
             *
             *  using (Microsoft.Data.SqlClient.SqlCommand command = new Microsoft.Data.SqlClient.SqlCommand())
             *  {
             *      command.Connection = cn;
             *      command.CommandTimeout = 300000;
             *      command.CommandText = "INSERT INTO [dbo].[ClientCredential]([ClientID],[ClientSecretHash]" +
             *                            ",[AllowedGrantTypes],[AllowOfflineAccess],[RefreshTokenUsage]" +
             *                            ",[AccessTokenLifetime],[RefreshTokenExpiration],[AbsoluteRefreshTokenLifetime])" +
             *                            "VALUES(@ClientID,@ClientSecretHash,@AllowedGrantTypes" +
             *                            ",@AllowOfflineAccess,@RefreshTokenUsage,@AccessTokenLifeTime" +
             *                            ",@RefreshTokenExpiration,@AbsoluteRefreshTokenLifetime);" +
             *                            "SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]; ";
             *
             *      //throw new Exception(command.CommandText);
             *
             *      command.Parameters.Add("@ClientID", System.Data.SqlDbType.NVarChar);
             *      command.Parameters["@ClientID"].Value = credential.ClientID;
             *
             *
             *
             *      command.Parameters.Add("@ClientSecretHash", System.Data.SqlDbType.NVarChar);
             *      string encryptedPassword = Helper.EncryptDecrypt.EncryptString(credential.ClientSecret);
             *
             *      command.Parameters["@ClientSecretHash"].Value = encryptedPassword.Replace("'", "''");
             *
             *      command.Parameters.Add("@AllowedGrantTypes", System.Data.SqlDbType.NVarChar);
             *      command.Parameters["@AllowedGrantTypes"].Value = credential.AllowedGrantTypes;
             *
             *
             *      command.Parameters.Add("@AllowOfflineAccess", System.Data.SqlDbType.Int);
             *
             *      if (credential.AllowOfflineAccess == null)
             *          command.Parameters["@AllowOfflineAccess"].Value = DBNull.Value;
             *      else
             *          command.Parameters["@AllowOfflineAccess"].Value = credential.AllowOfflineAccess;
             *
             *
             *      command.Parameters.Add("@RefreshTokenUsage", System.Data.SqlDbType.Int);
             *      command.Parameters["@RefreshTokenUsage"].Value = credential.RefreshTokenUsage;
             *
             *      command.Parameters.Add("@AccessTokenLifeTime", System.Data.SqlDbType.Int);
             *      command.Parameters["@AccessTokenLifeTime"].Value = credential.AccessTokenLifetime;
             *
             *
             *      command.Parameters.Add("@RefreshTokenExpiration", System.Data.SqlDbType.Int);
             *      command.Parameters["@RefreshTokenExpiration"].Value = credential.RefreshTokenExpiration;
             *
             *      command.Parameters.Add("@AbsoluteRefreshTokenLifetime", System.Data.SqlDbType.Int);
             *      command.Parameters["@AbsoluteRefreshTokenLifetime"].Value = credential.AbsoluteRefreshTokenLifetime;
             *
             *
             *      try
             *      {
             *          var obj = await command.ExecuteNonQueryAsync();
             *          //credential.Id = id;
             *
             *      }
             *      catch
             *      {
             *          throw;
             *      }
             *
             *      return credential;
             *
             *
             *
             *
             *  }
             * }*/
        }