Retrieve username from logon environment.
The following is the most common code snippet I offer to people. The original authors are mentioned in the post below.
If the username and password are different from the one the user is currently logged in as, there's no way for InfoPath to access the authentication information. If the username is the same as the currently logged in user, you can use code like this:
//Create a WScript.Network object, which provides access to the user data
var objNetwork = new ActiveXObject("WScript.network");
//Retrieve the UserName and write it into the my:UserName field
//Retrieve the UserDomain and write it into the my:UserDomain field
//Retrieve the ComputerName and write it into the my:ComputerName field
NOTE: Internet Explorer's security settings will affect whether you can create and use the WScript.Network object.
Pasted from <http://www.infopathfaq.com/development.asp?postid=710>