Link to home
Start Free TrialLog in
Avatar of Stefan Motz
Stefan MotzFlag for United States of America

asked on

Capture LOGON_USER in ASP.NET MVC - C#

Hi Experts,

I'm trying to convert a Classic ASP application to .NET MVC.

I'm having difficulties with identifying the logged in user. This is how I used to do it with Classic ASP:

<%=Request.ServerVariables("LOGON_USER")%>

With the code below I captured the logged in users's First Name, Last Name and Employee #.

<%
set rs = Server.CreateObject("ADODB.recordset")
Dim UserID
UserID = UCase(Request.ServerVariables("LOGON_USER"))
UserID = Replace(UCase(UserID), "\U", "\")

if InStr( UserID, "U" ) >= 0 then
    UserID = Replace( UserID, "U", "" )
end if

if InStr( UserID, "." ) > 0 then
        UserID = Mid( UserID, InStr( UserID, "\" ) + 1, InStr( UserID, "." ) - InStr( UserID, "\" ) - 1 )
    else
        UserID = Right( UserID, Len( UserID ) - InStr( UserID, "\" ) )
    end if

rs.Open "exec sp_Authentication'" & UserID & "'", Conn

do until rs.EOF
ID = rs("ID")
EMP_ID = rs("EMP_ID")
ALT_ID = rs("ALT_ID")
FIRST_NAME = rs("FIRST_NAME")
LAST_NAME = rs("LAST_NAME")


rs.MoveNext
loop
rs.Close
Set rs = Nothing
%>




<%=FIRST_NAME%> <%=LAST_NAME%> <%=EMP_ID%>

Open in new window


Users have the option to log in with their EMP_ID or ALT_ID


In my MVC project I can display the LOGON_USER like this:

string EmployeeLogin = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
ViewBag.Authentication = EmployeeLogin;




When they are logged in with their EMP_ID it returns the following:

ABCDDE\U123456  - The EMP_ID is a six digit number

When they are logged in with their ALT_ID this is what is returned:

ABC\124B8   - The ALT_ID can be a combination of letters and numbers after the \


How can I display the users' names in C#?

This is my stored procedure:
ALTER PROCEDURE [dbo].[sp_Authentication]
@empID varchar(15)
AS
BEGIN
SELECT [ID],
[EMP_ID], 
[ALT_ID], 
[FIRST_NAME], 
[LAST_NAME], 

     
FROM dbo.[Authentication]
where EMP_ID=@empID OR ALT_ID=@empID;
END

Open in new window


I would appreciate your help.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Stefan Motz

ASKER

Thank you for your reply. My only problem now is that I can't apply it to my MVC project, because the code is different in MVC. I'm still struggling with it, trying to figure out.
Thank you very much.