简体   繁体   English

WEB API - 如何使用身份为用户中的自定义属性添加值

[英]WEB API - how to add value to custom property in User using identity

Using Identity with WEB API i have created some users that are saved in SQL database.将身份与 WEB API 结合使用,我创建了一些保存在 SQL 数据库中的用户。 I have added custom property (ShippingAddress) to the already default properties that come with the User table created with Identity.我已将自定义属性 (ShippingAddress) 添加到使用 Identity 创建的用户表附带的默认属性中。

When i create the user i put : username, email and password to create the user.当我创建用户时,我输入:用户名、电子邮件和密码来创建用户。 I want the shipping address to be added later on.我希望稍后添加送货地址。

I need to add the shipping address to the user that is currently logged in. (I have no problems logging in or out).我需要向当前登录的用户添加送货地址。(我登录或退出都没有问题)。

Here is what i have tried:这是我尝试过的:

When I click this Button i get the username in alert box that is currently logged in. This works.当我单击此按钮时,我会在当前登录的警报框中获取用户名。这是有效的。

 <button id="GetUserName">GetUserName</button>

 $('#GetUserName').click(function () {
        $.ajax({
                url: 'GetUserName',
                method: 'GET',
                headers: {
                    'Authorization': 'Bearer '
                        + sessionStorage.getItem("accessToken")
                },
                success: function (data) {
                    alert(data);
                },
                error: function (jQXHR) {
                }
            });
        });

This Does not work.这不起作用。 The Button for Adding the Shipping Address is:添加收货地址的按钮是:

 <button id="addAddress">addAddress</button>

   $('#addAddress').click(function () {
            $.ajax({
                url: 'addAddress',
                method: 'PUT',
                headers: {
                    'Authorization': 'Bearer '
                        + sessionStorage.getItem("accessToken")
                },
                //success: function (data) {
                //    alert(data);
                //},
                error: function (jQXHR) {
                }
            });
        });

This gives me this error:这给了我这个错误:

jquery-3.3.1.min.js:2 PUT http://localhost:64687/addAddress 500 (Internal Server Error) jquery-3.3.1.min.js:2 PUT http://localhost:64687/addAddress 500(内部服务器错误)

These are the controllers:这些是控制器:

using EmployeeService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace EmployeeService.Controllers
{
    public class EmployeesController : ApiController
    {
        ApplicationDbContext db = new ApplicationDbContext();


        [Route("GetUserName")]
        public string GetName()
        {
            string x = User.Identity.Name;
            return x;
        }

        [Route("addAddress")]
       [HttpPut]
        public void addAddress()
        {
            string x = User.Identity.Name;
            var myUser =  db.Users.Find(x);
            myUser.ShippingAdress = "asdasd";
            db.SaveChanges();
        }
    }
}

I dont know if my problem is in the ajax request, the controller or both.我不知道我的问题是在 ajax 请求、控制器还是两者都有。 Can someone please help.有人可以帮忙吗。

The problem here was in the addAddress() controller;这里的问题出在 addAddress() 控制器中;

string x = User.Identity.Name;   // here is the problem
var myUser =  db.Users.Find(x); // here is the problem
myUser.ShippingAdress = "asdasd";

The method .Find() works only with primary keys. .Find() 方法仅适用于主键。 In my situation i didn't use the primary key.在我的情况下,我没有使用主键。 I don't know why it didn't give me any errors in the visual studio but the error occurred in the browser console.我不知道为什么它在 Visual Studio 中没有给我任何错误,但错误发生在浏览器控制台中。 Anyway... if i change it like this everything works fine.无论如何......如果我像这样改变它一切正常。

using Microsoft.AspNet.Identity;
...

string x = User.Identity.GetUserId(); // we changed this
var myUser =  db.Users.Find(x); 
myUser.ShippingAdress = "asdasd";

We need to add "using Microsoft.AspNet.Identity;"我们需要添加“使用 Microsoft.AspNet.Identity;” for the method .GetUserId() to work.方法 .GetUserId() 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用.map添加对象的自定义属性 - How to add custom property of object using .map 如何使用Web Speech API添加语音 - How to add a voice using Web Speech API 使用 Typescript 时如何在 Express req object 中添加自定义属性 - How to add custom property in Express req object when using Typescript 如何在 Quasar 中添加自定义属性? - How to add custom property in Quasar? 如何使用反应钩子 useState 从 API 向对象添加属性 - How to add property to an object from an API using react hooks useState 如何使用Web Api操作AddSolutionComponent使用javascript将实体添加到解决方案中? - How to add an entity to a solution with javascript using Web Api action AddSolutionComponent? 如何使用函数将属性名称和值添加到JavaScript对象? - How to add a Property Name and Value to a JavaScript Object using a function? 在 Google Directory API 中,如何使用 javascript 添加用户? - In Google Directory API, how do I add a user using javascript? 如何使用 Discord API 代表用户添加对消息的反应? - How to add a reaction to a message on user behalf using Discord API? 如何使用 VueJS 获取 json web API 的值 - How to get value of json web API using VueJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM