Sunday, May 27, 2018

How to get a value from Active Directory using C# PrincipalSearcher Class

Here is a sample C# program to get email id from active directory by username using PrincipalSearcher class.
using System.DirectoryServices.AccountManagement;
public string GetEmailIdFromActiveDirectory(string userName)
{
var emailId = string.Empty;

string activeDirectory_LDAP = "server";
string activeDirectory_User = "ad_username";
string activeDirectory_Password = "ad_password";

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, activeDirectory_LDAP);

bool isCredentialsValid = ctx.ValidateCredentials(activeDirectory_User, activeDirectory_Password);

if (isCredentialsValid)
{
UserPrincipal userPr = new UserPrincipal(ctx);
userPr.SamAccountName = userName;

PrincipalSearcher srchUser = new PrincipalSearcher(userPr);
UserPrincipal foundUsr = srchUser.FindOne() as UserPrincipal;

if (foundUsr != null)
{
emailId = foundUsr.Email;
}
}

return emailId;
}

Friday, March 30, 2018

Create custom SQL exception with custom message in C# (CSharp)

Below is a sample code which is used to create a custom SQL exception with custom message. The parameter accepts the message to be thrown. It is mostly used in Unit testing scenario where you need to validate for a custom message.

Sample Code
private SqlException GetSqlException(string message)
{
    SqlErrorCollection collection = Construct();
    SqlError error = Construct(-2, (byte)2, (byte)3, "Server", message, "Prcedure", 100, (uint)1);

    typeof(SqlErrorCollection)
        .GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance)
        .Invoke(collection, new object[] { error });

    var e = typeof(SqlException)
        .GetMethod("CreateException", BindingFlags.NonPublic | BindingFlags.Static, null, CallingConventions.ExplicitThis, new[] { typeof(SqlErrorCollection), typeof(string) }, new ParameterModifier[] { })
        .Invoke(null, new object[] { collection, "11.0.0" }) as SqlException;

    return e;
}
private T Construct(params object[] p)
{
    return (T)typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0].Invoke(p);
}

Sunday, March 4, 2018

What is the Difference between String and string in C#?

String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. At execution time there is no difference. As far as guidelines, it's recommended to use string any time you're referring to an object.

Example:
string place = "Hello World";
It's generally recommended to use String if you need to refer specifically to the class.

Example:
var place = 'World!'
string message = String.Format("Hello {0}!", place);
Similarly for other datatypes,
object:  System.Object
string:  System.String
bool:    System.Boolean
byte:    System.Byte
sbyte:   System.SByte
short:   System.Int16
ushort:  System.UInt16
int:     System.Int32
uint:    System.UInt32
long:    System.Int64
ulong:   System.UInt64
float:   System.Single
double:  System.Double
decimal: System.Decimal
char:    System.Char