1

I have an application and I can add in this application. I need to save the Id number of the user who made the addition in my database. I do the insertion with AJAX and Jquery. But I do not know how to access the Id of the logged in user and post it.

Can you help?

This is my Jquery and Ajax Codes

  $(document).on('click', '#addButton', function () {



    //
    var DriverName = $('#AddModal #driverName').val();
    var PlateNumber = $('#AddModal #plateNumber').val();
    var TrailerNumber = $('#AddModal #trailerNumber').val();
    var SealNumber = $('#AddModal #sealNumber').val();
    var ProcessDate = $('#AddModal #processDate').val();
    var ProcessType = $('#AddModal #ProcessTypeName option:selected').val();
    var Personels = $('#AddModal #personels option:selected').val();
    var ReasonToVisit = $('#AddModal #reasonTo').val();
    var CreatedUserId=@User

    //
    var ItemLoadModels = new Array();
    var VehicleLoginCreate = {};

    //
    VehicleLoginCreate.DriverName = DriverName;
    VehicleLoginCreate.PlateNumber = PlateNumber;
    VehicleLoginCreate.TrailerNumber = TrailerNumber;
    VehicleLoginCreate.SealNumber = SealNumber;
    VehicleLoginCreate.ProcessDate = ProcessDate;
    VehicleLoginCreate.ProcessTypeId = ProcessType;
    VehicleLoginCreate.PersonnelId = Personels;
    VehicleLoginCreate.ReasonToVisit = ReasonToVisit;
    VehicleLoginCreate.ItemLoadModels = ItemLoadModels;

    //.
    $("#AddItemsTable tr:not(:first)").each(function () {

        let ItemLoadModel = {
            Id: "", RowId: "",   OrderNo: "", ItemName: 0, DeclarationNumber: "", T1Number: "", ShipperId: "", ConsigneeId: "", CaseCount: "", CaseCountType: "",
            Weight: 0.00, WeightType: ""
        }

        ItemLoadModel.OrderNo = $('#AddModal #Order').val();
        ItemLoadModel.ItemName = $('#AddModal #Item').val();
        ItemLoadModel.DeclarationNumber = $('#AddModal #Declaration').val();
        ItemLoadModel.T1Number = $('#AddModal #T1').val();
        ItemLoadModel.ShipperId = $('#AddModal #shipperd').val();
        ItemLoadModel.ConsigneeId = $('#AddModal #consignee').val();
        ItemLoadModel.CaseCount = $('#AddModal #Case').val();
        ItemLoadModel.CaseCountType = $('#AddModal #caseType').val();
        ItemLoadModel.Weight = $('#AddModal #Weight').val();
        ItemLoadModel.WeightType = $('#AddModal #weightTypes').val();
        ItemLoadModel.Id = $('#AddModal #id').val();
        ItemLoadModel.RowId = $('#AddModal #rowId').val();
        ItemLoadModels.push(ItemLoadModel);

    });

    //
    console.log(VehicleLoginCreate);
    alert(VehicleLoginCreate);
    $.ajax({
        type: 'POST',
        url: '/VehicleLogin/AddVehicleLogin',
        data: VehicleLoginCreate,

        success: function (result) {

            alert('Ekleme İşlemi Başarılı.');
            console.log(result);
            setInterval('location.reload()', 700);
        },
        error: function () {
            alert('Ekleme İşlemi Başarısız.');
            console.log('Failed ');
        }


    });
});

I have an application and I can add in this application. I need to save the Id number of the user who made the addition in my database. I do the insertion with AJAX and Jquery. But I do not know how to access the Id of the logged in user and post it.

Sefa Köse
  • 11
  • 2
  • How ? Plase give me an example. – Sefa Köse Feb 24 '23 at 13:22
  • 1
    Don't post the user id from JavaScript. JavaScript code runs on the browser and a malicious user can easily change the value. Instead, you should get the user id in your controller. Check [How to get the current logged in user ID in ASP.NET Core?](https://stackoverflow.com/questions/30701006/how-to-get-the-current-logged-in-user-id-in-asp-net-core) – Dimitris Maragkos Feb 24 '23 at 13:24

1 Answers1

1

I need to save the Id number of the user who made the addition in my database. I do the insertion with AJAX and Jquery. But I do not know how to access the Id of the logged in user and post it.

As @Dimitris Maragkos said we shouldn't directly get the user ID at client js and then send it through the ajax.

The right action should be we need send the user login's token or else then post the token to the controller.

Like below:

If you are using the JWT token, you firstly set it inside the ajax and then get the user ID inside the controller by using the User.Identity.Name.

Ajax like below:

   $.ajax({
        type: "POST",
        url: "/api/my-api-endpoint",
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
        },
        data: model,
        success: function (result) {
            // Handle success
        },
        error: function (xhr, status, error) {
            // Handle error
        }
    });
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65