Here is the code through which you can set the application pool identity of an existing application pool to custom user ..
ServerManager serverMgr = new ServerManager();
ApplicationPool myAppPool = serverMgr.ApplicationPools[“DefaultAppPool”];
myAppPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
myAppPool.ProcessModel.UserName = textBoxUname.Text;
myAppPool.ProcessModel.Password = textBoxPasswd.Text;
serverMgr.CommitChanges();