I need to remove the prefix(domain name) if exist in front of a username from a session variable.


paramsql.Value = Session("ssNtUser")

say the domain is za

username is macupryk

I would like to remove the za\ so in the session variable I have

macupryk only but the domain name can change. Also there might not even be a domain name with a slash.


Thanks.

Dani AI

Generated

Thread by asked for stripping an optional domain prefix (for example turning "ZA\macupryk" into "macupryk"). and offered IndexOf/Substring and Split approaches, and suggested split as well. Those are fine for simple cases; the short helper below is a bit more defensive (null/empty handling, trims whitespace), prefers the last backslash when present, and also handles UPN-style names like "user@domain.com".

// C#
string UnqualifyUser(object sessionValue)
{
    var raw = Convert.ToString(sessionValue ?? string.Empty).Trim();
    if (raw.Length == 0) return string.Empty;

    int bs = raw.LastIndexOf('\\');
    if (bs >= 0) return raw.Substring(bs + 1);

    int at = raw.IndexOf('@');
    if (at > 0) return raw.Substring(0, at);

    return raw;
}

// example usage
paramSql.Value = UnqualifyUser(Session["ssNtUser"]);
' VB.NET
Function UnqualifyUser(sessionValue As Object) As String
    Dim raw As String = If(sessionValue, String.Empty).ToString().Trim()
    If raw.Length = 0 Then Return String.Empty

    Dim bs As Integer = raw.LastIndexOf("\"c)
    If bs >= 0 Then Return raw.Substring(bs + 1)

    Dim atPos As Integer = raw.IndexOf("@"c)
    If atPos > 0 Then Return raw.Substring(0, atPos)

    Return raw
End Function

' example usage
paramSql.Value = UnqualifyUser(Session("ssNtUser"))

Notes and quick checks: using LastIndexOf('\') avoids problems if the value somehow contains extra backslashes; trimming and null/empty checks prevent exceptions; handling '@' covers UPN formats. If running on newer frameworks, string.IsNullOrWhiteSpace can be used instead of the Length check. Since the result goes to a DB parameter, keep using parameterized commands and set the parameter type — do not concatenate into SQL. Example mappings: "ZA\macupryk" -> "macupryk", "macupryk@contoso.com" -> "macupryk", "macupryk" -> "macupryk", null/empty -> "". This complements the compact answers from and while being a bit more robust for real-world inputs.

Recommended Answers

All 4 Replies

string testString = Session("ssNtUser").ToString();

int i = testString.IndexOf("\\")+1;
			if(i > 0)
			{
				testString=testString.Substring(i);
			}

You could also try just running a split command on the string if it contains a backslash.

I need to remove the prefix(domain name) if exist in front of a username from a session variable.


paramsql.Value = Session("ssNtUser")

say the domain is za

username is macupryk

I would like to remove the za\ so in the session variable I have

macupryk only but the domain name can change. Also there might not even be a domain name with a slash.


Thanks.

Hi,

U can use the split method of string object to get the username.

string strUser = Session("ssNtUser") as string;
string[] strTemp = strUser.Split('\\');
if(strTemp.length > 0)
strUser = strTemp[strTemp.length - 1];
paramSql.Value = strUser;

Thanks,
Kedar

vb.net : User.Identity.Name.Substring(User.Identity.Name.IndexOf("\") + 1)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.