SOL9 2.0 Class: CredentialsPrompt

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * CredentialsPrompt.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/04/10


#pragma once

#include <sol/Object.h>
#include <sol/String.h>

#include <wincred.h> 

#pragma comment (lib, "credui.lib")

namespace SOL {

class CredentialsPrompt :public Object {

    
public:
    /**
     * Constructor
     */
    CredentialsPrompt()
    {
    }    

public:
    /**
     * Input username and password from a command line(console).
     */
    int    show(__inout String& username, 
                __inout String& password, 
                __in_opt const TCHAR* targetForCredentials = _T("Server"),
                __in_opt BOOL fSave= FALSE, 
                __in_opt DWORD dwFlags = (CREDUI_FLAGS_DO_NOT_PERSIST|
                    CREDUI_FLAGS_EXCLUDE_CERTIFICATES) )
    {
        int rc = -1;
        TCHAR name[CREDUI_MAX_USERNAME_LENGTH+1];

        TCHAR passwd[CREDUI_MAX_PASSWORD_LENGTH+1];
        SecureZeroMemory(name, sizeof(name));
        SecureZeroMemory(passwd, sizeof(passwd));

        // If username parameter has been set, copy the string from username String to name buffer.
        int ulen = username.getLength();
        int plen = password.getLength();
        if (ulen >CREDUI_MAX_USERNAME_LENGTH) {
            //Trimming username
            ulen = CREDUI_MAX_USERNAME_LENGTH;
        }
        if (ulen> 0) {
            strncpy(name, (const TCHAR*)username, ulen);
        }

        // If password parameter has been set, copy the string from password String to passwd buffer.
        if (plen >CREDUI_MAX_PASSWORD_LENGTH) {
            //Trimming
            plen = CREDUI_MAX_PASSWORD_LENGTH;
        }
        if (plen> 0) {
            strncpy(passwd, (const TCHAR*)password, plen);
        }

        if ((rc = CredUICmdLinePromptForCredentials( 
                targetForCredentials,
                NULL,         // Reserved
                0,            // Reason
                name,  
                CREDUI_MAX_USERNAME_LENGTH+1, 
                passwd,
                CREDUI_MAX_PASSWORD_LENGTH+1,
                &fSave,  
                dwFlags))== NO_ERROR) {

            //OK
            username = name;
            password = passwd;

            SecureZeroMemory(name, sizeof(name));
            SecureZeroMemory(passwd, sizeof(passwd));
        } else {
            // May be Cancelled
            username = "";
            password = "";
        }
        return rc;
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.