SEARCH
TOOLBOX
LANGUAGES
modified on 19 June 2010 at 01:27 ••• 1,552 views

O2 Script/Ask user for security credentials (Username and Password)

From

Jump to: navigation, search

this script (Windows Forms Control) is a small pop-up window that asks the user for its a credential (username and password).

The returned data is of type ICredential (used in O2 to access security credentials)

screenshots

  • in O2's Simple Script Editor enter the following code
return ascx_AskUserForLoginDetails.ask();
 
//O2File:C:\O2\_XRules_Local\ascx_AskUserForLoginDetails.cs

Image:5_26_2010_4_08_15_PM_tmp3AC8.jpg

  • and click execute

Image:5_26_2010_4_08_53_PM_tmp3AC9.jpg

  • enter some values on the username and password field

Image:5_26_2010_4_09_36_PM_tmp3ACA.jpg

  • which you can see are now loaded into a ICredential object (show in the 'Ouput' window of O2's Simple Script Editor)

Image:5_26_2010_4_10_46_PM_tmp3ACB.jpg


source code

public class ascx_AskUserForLoginDetails : ContainerControl
    {       
	public string UserName { get;set;}
	public string Password { get;set;}
 
	public AutoResetEvent HaveAnswer { get;set;}
       public TextBox UserNameTextBox { get; set;}
       public TextBox PasswordTextBox { get; set;}
       public Button OKButton { get; set;}
 
	public static ICredential ask()
    	{
    		var loginDetailsGui = O2Gui.open<ascx_AskUserForLoginDetails>("Enter Login Details", 250,115);    	
    		loginDetailsGui.buildGui();    		   	
    		var credential = loginDetailsGui.getAnswer();    		
    		loginDetailsGui.close();
 
    		return credential;
    	}    	   	   	   	   
 
       public ascx_AskUserForLoginDetails()
    	{
    		HaveAnswer = new AutoResetEvent(false);
    		UserName = "";
    		Password = "";
    	}
 
    	public void buildGui()
    	{    								
			UserNameTextBox = this.add_Label("Username:",10,0)
							      .append_TextBox("");
 
			UserNameTextBox.onTextChange((text)=> UserName = text)
						   .align_Right(this);
 
			PasswordTextBox = this.add_Label("Password: ",35,0)
								  .append_TextBox("");
 
			PasswordTextBox.onTextChange((text)=> Password = text)
						   .align_Right(this);
 
			var OKButton = this.add_Button("OK", 60,0);							  
			OKButton.onClick(answerAvailable)
					.left(this.width() - OKButton.width() - 1)
					.anchor_BottomRight();
 
			this.parentForm().Closed += (sender,e) => answerAvailable();			
    	}
 
    	public ICredential getAnswer()
    	{
    		HaveAnswer.WaitOne();  
    		var credential = new Credential();
    		credential.UserName = UserName;
    		credential.Password = Password;
    		"u:{0}".debug(credential.UserName);
    		"p:{0}".debug(credential.Password);
    		return credential;
    	}
 
    	public void answerAvailable()
    	{	   		
    		HaveAnswer.Set();
    	}
 
    	public void close()
    	{
    		if (this.parentForm() != null)
				this.parentForm().Close();     		
    	}
 
     }
MediaWiki Appliance - Powered by TurnKey Linux