简体   繁体   English

带有承载身份验证的ASP.NET Web API。 如何获取/添加/更新唯一用户数据?

[英]ASP.NET Web API with Bearer authentication. How to get/add/update unique user data?

Currently I am about to develop my first REST web API! 目前我即将开发我的第一个REST Web API!

I'm currently designing how the system will work yet I am a little confused how things are going to work. 我目前正在设计系统如何工作但我有点困惑如何工作。

Primarily, a mobile app will consume the Web API, but it must be secure! 首先,移动应用程序将使用Web API, 但它必须是安全的!

For example I wouldn't want an un-authed request handled at all. 例如,我根本不想要处理未经过处理的请求。 (Apart from a user registering) (除了用户注册)

I have found that I can use SSL + Bearer tokens to achieve this user authentication. 我发现我可以使用SSL + Bearer令牌来实现此用户身份验证。 I am comfortable with this and have tested to see how this would work. 我对此很满意,并已经过测试,看看它是如何工作的。 And it's suitable. 它很合适。

The problem arises when I wish to retrieve the user details. 当我希望检索用户详细信息时出现问题。 In my system a user would log in to the mobile app which would request the token from the server. 在我的系统中,用户将登录到将从服务器请求令牌的移动应用程序。 If all is good, I can log the user into the app. 如果一切顺利,我可以将用户登录到应用程序中。 Great! 大! Now, I need to get the information stored about this user to present to them. 现在,我需要获取有关此用户的信息以呈现给他们。 ie name, email, reward points etc... 即姓名,电子邮件,奖励积分等......

I am unfamiliar with how to add extra user data AND retrieve it with the Web API. 我不熟悉如何添加额外的用户数据并使用Web API检索它。 I understand that the token can be used to uniquly identify a user. 我知道令牌可以用来唯一地识别用户。 but how can I extend this data? 但是我该如何扩展这些数据呢?

Currently I have not much more than a blank WebAPI project with the bearer token authentication implemented. 目前,我只有一个空白的WebAPI项目,实现了承载令牌认证。 Still using the Entity framework. 仍在使用Entity框架。 How can I add more fields of data to a user record? 如何向用户记录添加更多数据字段? Furthermore, how can I update these records? 此外,我如何更新这些记录? For example, a user has gained some reward points, how can update the user data for this? 例如,用户获得了一些奖励积分,如何为此更新用户数据?

One final question, Is this suitable for retaining per user data? 最后一个问题,这是否适合保留每个用户数据? ie can I link other data to a userID or something similar? 即我可以将其他数据链接到用户ID或类似的东西吗?

Apologies for sounding over-curious, I am very new to MVC 听起来好奇的道歉,我对MVC很新

The below code in the IdentityModel.cs would seem like the appropriate place to add user data, but how do I add to this? IdentityModel.cs中的以下代码似乎是添加用户数据的合适位置,但我该如何添加? for example adding a field for reward points? 例如为奖励积分添加一个字段? then how would I update upon it? 那么我将如何更新呢?

public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
            // Add custom user claims here
            return userIdentity;
        }
    }

How I would do it: 我该怎么做:

  • Create ActionFilter that would validate token, it would use your custom class that would leverage DI, it would obviously get the user ID as well 创建将验证令牌的ActionFilter,它将使用可以利用DI的自定义类,它显然也会获得用户ID

  • Now that you know that user is authenticated and you know the user ID just do your regular CRUD based on this user ID 现在您知道用户已经过身份验证,并且您知道用户ID只是根据此用户ID执行常规CRUD

Note: Remember to also validate the payload, since if I send you PUT: /user/887/points {points: 999} I could potentially gain unlimited points. 注意:请记住还要验证有效负载,因为如果我发给你PUT: /user/887/points {points: 999}我可能获得无限积分。

It's not necessary to use ASP .NET Identity for implementing security in your web API project. 没有必要使用ASP .NET Identity在Web API项目中实现安全性。

If you use Identity you will have to stick on to "ApplicationUser" and Identity tables for user management where you won't be able to complete your requirement. 如果您使用Identity,您将不得不坚持使用“ApplicationUser”和用户管理的身份表,否则您将无法完成您的要求。

A solution is to handle user management with your own custom table and implement security using OWIN middleware classes available for .NET, ie, you need to write code for generating and validating tokens rather than using Identity. 解决方案是使用您自己的自定义表处理用户管理,并使用可用于.NET的OWIN中间件类实现安全性,即,您需要编写用于生成和验证令牌的代码,而不是使用Identity。

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

相关问题 如何使用两个asp.net web api的持票令牌 - How to use bearer token with two asp.net web api 在ASP.net身份中向Asp.Net Web API 2承载令牌添加声明? - Add Claims to Asp.Net Web API 2 Bearer Token in ASP.net Identity? 如何实现允许承载令牌身份验证或 api 密钥身份验证的 ASP.Net Core API Controller - How to implement an ASP.Net Core API Controller which allows Bearer Token authentication OR api key authentication Asp.net Core 2.1 将 AzureAd 身份验证承载传递到使用 [Authorize] 的 Web Api - Asp.net Core 2.1 Pass AzureAd Authentication Bearer to Web Api which uses [Authorize] ASP.NET JWT承载身份验证不会在请求上下文中更改用户 - ASP.NET JWT bearer authentication not changing user in request context ASP.NET Core 5 Web API中的用户认证 - Authentication of a user in ASP.NET Core 5 Web API 如何在基于 cookie 身份验证的 asp.net mvc 项目中向 web api 添加令牌身份验证 - How to Add token authentication to web api in cookie authentication based asp.net mvc project Angular 4-如何从ASP.Net Web API获取数据 - Angular 4 - How to get data from ASP.Net web api 如何在 Asp.Net Core 6 中向类型化的 HttpClient 添加不记名令牌身份验证 - How to add bearer token authentication to typed HttpClient in Asp.Net Core 6 ASP.NET 核心 web api 编辑承载认证 - ASP.NET Core web api editing Bearer auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM