?
Solved

Displaying Users Name

Posted on 2008-11-19
11
Medium Priority
?
395 Views
Last Modified: 2013-12-17
I have an urgent issue that I need to fix. At this time, my web page (written in Asp.net/C#) displays their Windows Login ID on the top of the web page.  For example, when I visit the web page, it says "Hello Domain\UserID".  

I wanted to ask if there is a way that I can have it say the users name instead.  The web page uses the ASP.NET security tool and is configured for a local network.  I have posted the code that displays the UserID below.  I am hoping that there are some built in functions to display the name in ASP.NET.

 If it is not possible to obtain the name using the built in security/roles function in ASP. net, could you please let me know how I can get the UserID returned to a query and bring it back to the page?  I have a query that can take the userID and get the user's name.  I would need step-by-step instructions.

<LoggedInTemplate>
               <div id="welcomebox">
                  <asp:LoginName ID="LoginName1" runat="server" FormatString="Hello	{0}" meta:resourcekey="LoginName1Resource1" /><br />
                  <small>
          
               </div>
            </LoggedInTemplate>

Open in new window

0
Comment
Question by:jjrr007
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22998320
Hi there,

To get the user name, smiply use this:

Dim userName As String
If HttpContext.Current.Request.IsAuthenticated Then
   userName = HttpContext.Current.User.Identity.Name
End If

Open in new window

0
 
LVL 1

Author Comment

by:jjrr007
ID: 22998464
Thanks.  that returned the WindowsID.  I am creating the from using C#/Asp.net.  
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22998539
No, that's the UserName for the user currently authenticated, not the Windows ID. The WindowsID is a SID value.
 
The get the UserId (using the ASP.NET security), you would use the following:

Dim guid as Guid =   Ctype( Membership.GetUser.ProviderUserKey, Guid)

Another way to get the user name (what you call WindowsID), is:

Dim userName as String =   Membership.GetUser.UserName

If that line returned "DOMAIN\USERNAME" is because that's just what the current user name is.
Now, are you storing the "real" user first and last names somewhere else (like, Profile)? Is that what you need?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:jjrr007
ID: 22999512
We are getting closer, but not there yet.  I have a query that can show the employee Name and takes what is returned by HttpContext.Current.User.Identity.Name.  
The problem that I am facing now is that when the Detail View loads, it can't get the default value (from HttpContext.Current.User.Identity.Name).  This is how I am trying to send the default value by this code:
 protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters.Add("User", HttpContext.Current.User.Identity.Name);
}
What do you suggest that I change?  Thanks
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22999597
I think that you want a SelectParamenter, not an UpdateParameter:

protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("User", HttpContext.Current.User.Identity.Name);
}

Open in new window

0
 
LVL 1

Author Comment

by:jjrr007
ID: 22999747
Thanks.  When I did that, I received the following message:
Error 10 The name 'SqlDataSource1' does not exist in the current context C:\.master.cs
I am using a master page, does that make a difference?  What do you suggest or maybe another alternative way of doing this?
 
0
 
LVL 20

Expert Comment

by:Juan_Barrera
ID: 22999766
Where is your SqlDataSource located?
Just add the above line t othe page / masterpage where the DS is located.
0
 
LVL 1

Author Comment

by:jjrr007
ID: 22999804
Here is the code, the datasource code is highlighed.  I pasted from the div item to the top of the page.  I pasted the c# code in the snippet.  I generated the error with the code.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Template.master.cs" Inherits="MB.NEAreaBusinessEfficiencyTeamWebForum.UI.Template" %>
<%@ Register Src="Controls/ThemeSelector.ascx" TagName="ThemeSelector" TagPrefix="mb" %>
<%@ Register Src="Controls/PollBox.ascx" TagName="PollBox" TagPrefix="mb" %>
<%@ Register Src="Controls/PersonalizationManager.ascx" TagName="PersonalizationManager" TagPrefix="mb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>NE Area Business Efficiency Team</title>
</head>
<body>
<form id="Main" runat="server">
<div id="header">
<div id="header2">
<div id="headermenu" > 

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:Menu ID="mnuHeader" runat="server" CssClass="headermenulink" DataSourceID="SiteMapDataSource1"
MaximumDynamicDisplayLevels="0" Orientation="Horizontal" SkipLinkText="" StaticDisplayLevels="2" Style="text-align: center; margin-left: 90px;">
</asp:Menu>
</div>
</div>
<div id="loginbox">
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Login ID="Login" runat="server" Width="100%" FailureAction="RedirectToLoginPage" meta:resourcekey="LoginResource1">
<LayoutTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="60px"><asp:Label runat="server" ID="lblUserName" AssociatedControlID="UserName" Text="Username:" meta:resourcekey="lblUserNameResource1" /></td>
<td><asp:TextBox id="UserName" runat="server" Width="95%" meta:resourcekey="UserNameResource2" /></td>
<td width="5px" align="right">
<asp:RequiredFieldValidator ID="valRequireUserName" runat="server" SetFocusOnError="True"
ControlToValidate="UserName" Text="*" ValidationGroup="Login" meta:resourcekey="valRequireUserNameResource1" />
</td>
</tr>
<tr>
<td><asp:Label runat="server" ID="lblPassword" AssociatedControlID="Password" Text="Password:" meta:resourcekey="lblPasswordResource1" /></td>
<td><asp:TextBox ID="Password" runat="server" TextMode="Password" Width="95%" meta:resourcekey="PasswordResource2" /></td>
<td width="5px" align="right">
<asp:RequiredFieldValidator ID="valRequirePassword" runat="server" SetFocusOnError="True"
ControlToValidate="Password" Text="*" ValidationGroup="Login" meta:resourcekey="valRequirePasswordResource1" />
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><asp:CheckBox ID="RememberMe" runat="server" Text="Remember me" meta:resourcekey="RememberMeResource1"></asp:CheckBox></td>
<td align="right">
<asp:ImageButton ID="Submit" runat="server" AlternateText="Login"
CommandName="Login" ImageUrl="~/images/go.gif" ValidationGroup="Login" meta:resourcekey="SubmitResource1" />
</td>
<td width="5px" align="right">&nbsp;</td>
</tr>
</table>

</LayoutTemplate>
</asp:Login>
</AnonymousTemplate>
<LoggedInTemplate>
<div id="welcomebox">
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
&nbsp;<asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>'></asp:Label><br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbWebForumConnectionString %>"
SelectCommand="tbh_Forums_GetUserName" SelectCommandType="StoredProcedure">

</asp:SqlDataSource>

<asp:LoginName ID="LoginName1" runat="server" FormatString="Hello {0}" meta:resourcekey="LoginName1Resource1" /><br />
<small>
</div>
</LoggedInTemplate>
</asp:LoginView>

</div>
</asp:PlaceHolder>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
namespace NameTakenOutforPrivacy.UI
{
    public partial class Template : System.Web.UI.MasterPage
    {
     
 
        private bool _enablePersonalization = false;
        public bool EnablePersonalization
        {
            get { return _enablePersonalization; }
            set
            {
                _enablePersonalization = value;
 
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters.Add("User", HttpContext.Current.User.Identity.Name);
        }
 
        protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
 
        }
        protected void PollBox1_Load(object sender, EventArgs e)
        {
 
        }
}
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
Juan_Barrera earned 2000 total points
ID: 22999822
Place the datasource outside the template, like here:

</asp:LoginView> 
  <asp:SqlDataSource ID="SqlDataSource1" runat="server" "ConnectionString="<%$ ConnectionStrings:dbWebForumConnectionString %>"
SelectCommand="tbh_Forums_GetUserName" SelectCommandType="StoredProcedure">
</asp:SqlDataSource> 
</div> 

Open in new window

0
 
LVL 1

Author Comment

by:jjrr007
ID: 22999901
That worked! I will remember to place the data sets outside of the templates from now on.  Thanks again!
0
 
LVL 1

Author Closing Comment

by:jjrr007
ID: 31518426
I wanted to thank you for your assistance, time and expertise with this question and others.  You are extremely knowledgeable!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question