locked
Get logged in User and Insert username into database table RRS feed

  • Question

  • User-1559458579 posted

    Visual Studio 2019
    ASP.Net 4.7
    Language C#
    Using Code Behind
    Identity
    Insert Method - SQLDataSource using - protected void Page_Load(object sender, System.EventArgs e)

    1. {
    2. InsertCompany.Insert();
    3. }


    I have created a table and I will be inserting records in a database through the asp.net web application. My issue is I want to insert the value of the logged user with the record that is being created. If a user enters Company information I want the UserId field to be populated with the logged in user for the record being inserted. I have gone as far as I can go with this code. My plan is to assign the logged in user value to a field on the submit form. Below is the code I have so far but I'm not able to get it any further. If you can help please supply the code as telling me what would be best to do won't help me much. I would need the actual code. Also, I'm fairly certain the code I provided is not correct. I wanted to attempt to get it before I reached out. I have already successfully created inserting functionality. I simply need to add the additional field of UserID as part of the Insert.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;



    namespace WebApplication13
    {
    public partial class Backoffice_main : System.Web.UI.Page
    {
    protected void Page_Load(object sender, System.EventArgs e)
    {
    string UserId;

    1. _ = HttpContext.Current.User.Identity.Name;
    2. if (!User.Identity.IsAuthenticated)
    3. return;
    4. UserId = User.Identity.ToString
    5. }
    6. }
    7. Also trying this:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Security.Claims;
      using System.Security.Authentication;
      using System.Web.Security;
      using Microsoft.AspNet.Web;

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


      //{
      // InsertCompany.Insert();
      //}

      protected void Page_Load(object sender, System.EventArgs e)
      {

      if (User.Identity.IsAuthenticated)
      Label1.Text = User.Identity.Name;
      else
      Label1.Text = "No user identity available.";
      }


      }
      }

    Monday, August 17, 2020 11:31 PM

All replies

  • User-1330468790 posted

    Hi dmartinm, 

     

    Could you please format the description and the codes?

    I think it would be much helpful to read and understand your question so that we could target the problem much easier.

     

    Thank you for understanding.

    Best regards,

    Sean

    Wednesday, August 19, 2020 8:02 AM