Show Authentication Dialog in C# for Windows Vista/7

Show Authentication dialog in C# for windows Vista/7

I managed to implement a solution that is working for me.

Here is the source code:

    [DllImport("ole32.dll")]
public static extern void CoTaskMemFree(IntPtr ptr);

[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;
}

[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);

[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);

public static void GetCredentialsVistaAndUp(string serverName, out NetworkCredential networkCredential)
{
CREDUI_INFO credui = new CREDUI_INFO();
credui.pszCaptionText = "Please enter the credentails for " + serverName;
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))
{
//TODO: ms documentation says we should call this but i can't get it to work
//SecureZeroMem(outCredBuffer, outCredSize);

//clear the memory allocated by CredUIPromptForWindowsCredentials
CoTaskMemFree(outCredBuffer);
networkCredential = new NetworkCredential()
{
UserName = usernameBuf.ToString(),
Password = passwordBuf.ToString(),
Domain = domainBuf.ToString()
};
return;
}
}

networkCredential = null;
}

I still need to work out the fine details such as how to remember the last credentials that were entered etc...

But the major part works.

How to show Windows Login Dialog?

Looks similar to Show Authentication dialog in C# for windows Vista/7

CredUIPromptForWindowsCredentials

C# - Log In By Using That Windows Credentials Dialog

I assume you mean this window

enter image description here

Searching StackOverflow I found the following article

UAC-style elevated prompt

So by the looks of it you can hook into the relevant API's but the amount of work required to replicate the UAC form and authentication

The best idea maybe to style a form like one and use your own logic or the API

And then have authorisation on certain methods based on the users Active Directory credentials

C#: How to create a window like this?

You want to create a dialog exactly like the first dialog you've shown, the one used by FluffyApp?

You'll have to create it yourself, by hand. It's not a standard Windows dialog; it's a custom dialog resource provided by the FluffyApp application. It's obviously modeled to look a lot like the standard Windows authentication dialog, which is a good idea—users are already familiar with the native UI and will find your application to be much easier to use if it strongly resembles what they're already accustomed to. I recommend that if you decide to create your own custom dialog that you follow Windows's example as well.

But it's not entirely clear why you need your dialog to look exactly like the one that FluffyApp uses. I'm not really even sure why FluffyApp needed to create a custom dialog! It seems like the standard Windows authentication dialog would be perfectly sufficient. They have the same number of input fields, the UI designers at Microsoft have just replaced labels with cue banners. Not anything to worry about.

How to show authentication dialog in C# .Net 3.5 SP1

How do you currently access the share? Through a UNC or do you first map it to a drive letter? One idea is to map it with the wnetaddconnection2 api call with the CONNECT_INTERACTIVE and CONNECT_PROMPT flags.

Windows Security Custom login validation

You'll find a complete implementation for WPF and WinForms using CredUIPromptForWindowsCredentials at Ookii dialogs.

How should one manage authentication on custom line of business software

I think we have resolved the issue for now. SteveS posted on my IT Security Exchange question with a nice idea about using WCF, WIF and ASP.NET Membership Provider. It's not ideal seeing as I'll be writing some of the code, so it's prolly gonna be insecure.

However it seems like the best option as its leveraging security technology built by Microsoft, and I'm just sewing it all together.

I'm just worried about the seams.



Related Topics



Leave a reply



Submit