Custom PSHost, Get-Credential cmdlet in C# -


i trying implement custom pshost class purpose of crafting guis power shell scripts. have taken code here base , started adapting gui projects https://msdn.microsoft.com/en-us/library/ee706557%28v=vs.85%29.aspx

all fine until tried run get-credential cmdlet , got error method not implemented(ooouuuu)... initial code these 2 methods:

public override pscredential promptforcredential(                                                  string caption,                                                   string message,                                                   string username,                                                   string targetname) {   throw new notimplementedexception(                        "the method or operation not implemented."); }  public override pscredential promptforcredential(                                    string caption,                                     string message,                                     string username,                                     string targetname,                                     pscredentialtypes allowedcredentialtypes,                                     pscredentialuioptions options) {   throw new notimplementedexception(                           "the method or operation not implemented."); } 

so after research implemented dialogue this:

    [dllimport("ole32.dll")]     public static extern void cotaskmemfree(intptr ptr);      [dllimport("credui.dll", charset = charset.auto)]     private static extern int creduipromptforwindowscredentials(ref credui_info notusedhere, int autherror, ref uint authpackage, intptr inauthbuffer, uint inauthbuffersize, out intptr refoutauthbuffer, out uint refoutauthbuffersize, ref bool fsave, int flags);      [dllimport("credui.dll", charset = charset.auto)]     private static extern bool credunpackauthenticationbuffer(int dwflags, intptr pauthbuffer, uint cbauthbuffer, stringbuilder pszusername, ref int pcchmaxusername, stringbuilder pszdomainname, ref int pcchmaxdomainame, stringbuilder pszpassword, ref int pcchmaxpassword);      [structlayout(layoutkind.sequential, charset = charset.auto)]     private struct credui_info     {         public int cbsize;         public intptr hwndparent;         public string pszmessagetext;         public string pszcaptiontext;         public intptr hbmbanner;     }      private enum promptforwindowscredentialsflags     {         ...     }      public override pscredential promptforcredential(                                        string caption,                                        string message,                                        string username,                                        string targetname,                                        pscredentialtypes allowedcredentialtypes,                                        pscredentialuioptions options)     {         credui_info credui = new credui_info();         credui.pszcaptiontext = "please enter credentails";         credui.pszmessagetext = "displayedmessage";         credui.cbsize = marshal.sizeof(credui);         uint authpackage = 0;         intptr outcredbuffer = new intptr();         uint outcredsize;         bool save = false;         int result = creduipromptforwindowscredentials(ref credui, 0, ref authpackage, intptr.zero, 0, out outcredbuffer, out outcredsize, ref save, 1 /* generic */);          var usernamebuf = new stringbuilder(100);         var passwordbuf = new stringbuilder(100);         var domainbuf = new stringbuilder(100);          int maxusername = 100;         int maxdomain = 100;         int maxpassword = 100;         if (result == 0)         {             if (credunpackauthenticationbuffer(0, outcredbuffer, outcredsize, usernamebuf, ref maxusername, domainbuf, ref maxdomain, passwordbuf, ref maxpassword))             {                 //clear memory allocated creduipromptforwindowscredentials                 cotaskmemfree(outcredbuffer);                 securestring securestring = new securestring();                 foreach (char c in passwordbuf.tostring())                 {                     securestring.appendchar(c);                 }                 return new pscredential(usernamebuf.tostring(), securestring);             }         }         return null;     } 

this works fine there 1 snag, when running get-credential cmdlet prompts value credential parameter, regardless of input after hitting return dialogue pops , works expected. doing same in ise dialogue pops directly without prompt. thought due arguments of method making them optional defining default value did not make difference. might due how cmdlet defined in ps environment question is: how can make running cmdlet jump straight dialogue? possible define default value credential parameter bind to? i'm guessing how ise gets around or missing something?

i took time , experimenting found it, mandatory parameter binding handled public override dictionary<string, psobject> prompt(string caption, string message, collection<fielddescription> descriptions) method in pshostuserinterface interface before promptforcredential called.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -