Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

creduiparseusername (credui)
 
.
Summary
Extracts the domain and user account name from a fully-qualified user name.

C# Signature:

[DllImport("credui.dll", EntryPoint="CredUIParseUserNameW", CharSet=CharSet.Unicode)]
private static extern CredUIReturnCodes CredUIParseUserName(
        string userName,
        StringBuilder user,
        int userMaxChars,
        StringBuilder domain,
        int domainMaxChars);

VB Signature:

TODO

User-Defined Types:

CredUIReturnCodes is a handy enum to use to interpret the integer return value.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

/// <summary>
/// Extracts the domain and user account name from a fully qualified user name.
/// </summary>
/// <param name="userName">A <see cref="string"/> that contains the user name to be parsed. The name must be in UPN or down-level format, or a certificate.</param>
/// <param name="user">A <see cref="string"/> that receives the user account name.</param>
/// <param name="domain">A <see cref="string"/> that receives the domain name. If <paramref name="userName"/> specifies a certificate, pszDomain will be <see langword="null"/>.</param>
/// <returns>
///     <see langword="true"/> if the <paramref name="userName"/> contains a domain and a user-name; otherwise, <see langword="false"/>.
/// </returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters")]
public static bool ParseUserName(string userName, out string user, out string domain)
{
    if (string.IsNullOrEmpty(userName))
    throw new ArgumentNullException("userName");

    StringBuilder userBuilder = new StringBuilder();
    StringBuilder domainBuilder = new StringBuilder();

    CredUIReturnCodes returnCode = NativeMethods.CredUIParseUserName(userName, userBuilder, int.MaxValue, domainBuilder, int.MaxValue);
    switch (returnCode)
    {
    case CredUIReturnCodes.NO_ERROR: // The username is valid.
        user = userBuilder.ToString();
        domain = domainBuilder.ToString();
        return true;

    case CredUIReturnCodes.ERROR_INVALID_ACCOUNT_NAME: // The username is not valid.
        user = userName;
        domain = null;
        return false;

    // Impossible to reach this state
    //case CredUIReturnCodes.ERROR_INSUFFICIENT_BUFFER: // One of the buffers is too small.
    //    throw new OutOfMemoryException();

    case CredUIReturnCodes.ERROR_INVALID_PARAMETER: // ulUserMaxChars or ulDomainMaxChars is zero OR userName, user, or domain is NULL.
        throw new ArgumentNullException("userName");

    default:
        user = null;
        domain = null;
        return false;
    }
}

Alternative Managed API:

TODO

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions