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.
romsomIT DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Perhaps something like this:
Default.aspx -
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EE_Q28710309._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    First Name: <%=AuthenticatedUser.FIRST_NAME%><br />Last Name: <%=AuthenticatedUser.LAST_NAME%><br />EMP ID: <%=AuthenticatedUser.EMP_ID%>
    </div>
    </form>
</body>
</html>

Open in new window

Default.aspx.cs -
using System;
using System.Linq;

namespace EE_Q28710309
{
	public partial class _Default : System.Web.UI.Page
	{
		EE_Q28710309_DataContext context;
		sp_Authentication_Result fAuthenticatedUser;

		public sp_Authentication_Result AuthenticatedUser { get { return fAuthenticatedUser; } }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (context == null)
					context = new EE_Q28710309_DataContext();
				fAuthenticatedUser = context.sp_Authentication(Request.LogonUserIdentity.Name.Contains(@"\") ? Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1) : Request.LogonUserIdentity.Name).FirstOrDefault();
			}
		}
	}
}

Open in new window

Using a generated function import in order to create the complex data type -
#region ComplexTypes
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmComplexTypeAttribute(NamespaceName = "EE_Q28710309_DataModel", Name = "sp_Authentication_Result")]
[DataContractAttribute(IsReference = true)]
[Serializable()]
public partial class sp_Authentication_Result : ComplexObject
{
	#region Factory Method

	/// <summary>
	/// Create a new sp_Authentication_Result object.
	/// </summary>
	/// <param name="id">Initial value of the ID property.</param>
	public static sp_Authentication_Result Createsp_Authentication_Result(global::System.Int32 id)
	{
		sp_Authentication_Result sp_Authentication_Result = new sp_Authentication_Result();
		sp_Authentication_Result.ID = id;
		return sp_Authentication_Result;
	}

	#endregion

	#region Primitive Properties

	/// <summary>
	/// No Metadata Documentation available.
	/// </summary>
	[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = false)]
	[DataMemberAttribute()]
	public global::System.Int32 ID
	{
		get
		{
			return _ID;
		}
		set
		{
			OnIDChanging(value);
			ReportPropertyChanging("ID");
			_ID = StructuralObject.SetValidValue(value);
			ReportPropertyChanged("ID");
			OnIDChanged();
		}
	}
	private global::System.Int32 _ID;
	partial void OnIDChanging(global::System.Int32 value);
	partial void OnIDChanged();

	/// <summary>
	/// No Metadata Documentation available.
	/// </summary>
	[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
	[DataMemberAttribute()]
	public global::System.String EMP_ID
	{
		get
		{
			return _EMP_ID;
		}
		set
		{
			OnEMP_IDChanging(value);
			ReportPropertyChanging("EMP_ID");
			_EMP_ID = StructuralObject.SetValidValue(value, true);
			ReportPropertyChanged("EMP_ID");
			OnEMP_IDChanged();
		}
	}
	private global::System.String _EMP_ID;
	partial void OnEMP_IDChanging(global::System.String value);
	partial void OnEMP_IDChanged();

	/// <summary>
	/// No Metadata Documentation available.
	/// </summary>
	[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
	[DataMemberAttribute()]
	public global::System.String ALT_ID
	{
		get
		{
			return _ALT_ID;
		}
		set
		{
			OnALT_IDChanging(value);
			ReportPropertyChanging("ALT_ID");
			_ALT_ID = StructuralObject.SetValidValue(value, true);
			ReportPropertyChanged("ALT_ID");
			OnALT_IDChanged();
		}
	}
	private global::System.String _ALT_ID;
	partial void OnALT_IDChanging(global::System.String value);
	partial void OnALT_IDChanged();

	/// <summary>
	/// No Metadata Documentation available.
	/// </summary>
	[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
	[DataMemberAttribute()]
	public global::System.String FIRST_NAME
	{
		get
		{
			return _FIRST_NAME;
		}
		set
		{
			OnFIRST_NAMEChanging(value);
			ReportPropertyChanging("FIRST_NAME");
			_FIRST_NAME = StructuralObject.SetValidValue(value, true);
			ReportPropertyChanged("FIRST_NAME");
			OnFIRST_NAMEChanged();
		}
	}
	private global::System.String _FIRST_NAME;
	partial void OnFIRST_NAMEChanging(global::System.String value);
	partial void OnFIRST_NAMEChanged();

	/// <summary>
	/// No Metadata Documentation available.
	/// </summary>
	[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
	[DataMemberAttribute()]
	public global::System.String LAST_NAME
	{
		get
		{
			return _LAST_NAME;
		}
		set
		{
			OnLAST_NAMEChanging(value);
			ReportPropertyChanging("LAST_NAME");
			_LAST_NAME = StructuralObject.SetValidValue(value, true);
			ReportPropertyChanged("LAST_NAME");
			OnLAST_NAMEChanged();
		}
	}
	private global::System.String _LAST_NAME;
	partial void OnLAST_NAMEChanging(global::System.String value);
	partial void OnLAST_NAMEChanged();

	#endregion
}
#endregion

Open in new window

Utilized by the following Imported Method in the datacontext -
/// <summary>
/// No Metadata Documentation available.
/// </summary>
/// <param name="empID">No Metadata Documentation available.</param>
public ObjectResult<sp_Authentication_Result> sp_Authentication(global::System.String empID)
{
	ObjectParameter empIDParameter;
	if (empID != null)
	{
		empIDParameter = new ObjectParameter("empID", empID);
	}
	else
	{
		empIDParameter = new ObjectParameter("empID", typeof(global::System.String));
	}
	return base.ExecuteFunction<sp_Authentication_Result>("sp_Authentication", empIDParameter);
}

Open in new window

From the following data set -Capture.JPGProduces the following output -Capture.JPG
How to create function import in ADO.NET.

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
romsomIT DeveloperAuthor Commented:
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.
0
romsomIT DeveloperAuthor Commented:
Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.