How to use script to "GUI login" in "OS X LION"

HI everyone,I can use a script as


/usr/bin/osascript <<AppleScript
tell application “System Events”
keystroke “username”
keystroke return
delay 1.0
keystroke “password”
delay 1.0
keystroke return
end tell
AppleScript

to remote log in via UI when I’m in mac 10.6, but now it can’t work with Lion,
got an “execution error:An error of type -10810 has occurred.(-10810)” message

then try another one which also works fine in 10.6:

osascript<<EOT

tell application “System Events”
tell process “SecurityAgent”
set value of text field 1 of group 1 of window 1 to “username”
set value of text field 2 of group 1 of window 1 to “password”
delay 0.5
key code 36
delay 0.5
key code 36
end tell
end tell
EOT

still got error,need your help ,thanks a lot ~