Tim Vavra
Tim Vavra

Reputation: 537

User name not getting to database

I am using the following to store the current user name in a variable that is passed to my stored procedure and then to my database. This is initiated with a button click.

string UserName = Page.User.Identity.Name.ToString();

The variable is sent with this command:

cmd.Parameters.AddWithValue("@UserName", UserName);

I have created a web form in Web Developer 2008 Express, and when I display the browser in Web Developer I get the desired results. When I run the web page from my native browser (Firefox) running on a Mac, I get nothing. Web Developer is running on a Windows 2008 Server installation. Do I need to do something different for Macs? I will have a majority of users who will use Mac rather than Windows.

Here is the code from the aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;



namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void MasterData_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            using (SqlConnection con = new SqlConnection("server=(local);database=InvoiceSHC;Trusted_Connection=Yes"))
            {

                // Get the UserId of the logged in user 

                string UserName = HttpContext.Current.User.Identity.Name.ToString();
                string Timestamp = DateTime.Now.ToString();
                //Not sure why you need a SqlDataAdapter unused here.
                //SqlDataAdapter DA = new SqlDataAdapter("UpdateSHCInvoice", con);
                SqlCommand cmd = new SqlCommand("UpdateSHCInvoice", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Ref", dlRef.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@PhaseName", dlPhase.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@PageType", dlPageType.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@Page", tbPage.Text);
                cmd.Parameters.AddWithValue("@Percent", dlPercent.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@ChngType", dlChngType.SelectedItem.Value);
                cmd.Parameters.AddWithValue("@UserName", UserName);
                cmd.Parameters.AddWithValue("@Timestamp",Timestamp);
                con.Open();
                cmd.ExecuteNonQuery();

                GridView1.DataBind();

            }



        }


    }
}

Upvotes: 1

Views: 127

Answers (1)

Josh Darnell
Josh Darnell

Reputation: 11433

I'm thinking that the user is not authenticated when accessing the page from a browser on another computer, but is authenticated when accessing from the server. You can check this for sure by setting the username like this:

string UserName = "";
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
    UserName = HttpContext.Current.User.Identity.Name.ToString();
}
else
{
    UserName = "UnauthenticatedUser";
}

If I am correct, then you need to double check your authentication procedure (web.config, etc). Let me now if you have any questions.

Upvotes: 1

Related Questions