ASP.NET (OWIN) 身份:如何从 Web API 控制器获取用户 ID? [英] ASP.NET (OWIN) Identity: How to get UserID from a Web API controller?

查看:27
本文介绍了ASP.NET (OWIN) 身份:如何从 Web API 控制器获取用户 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用 VS2013 RTW,ASP.NET MVC5)

(Using VS2013 RTW, ASP.NET MVC5)

我看过很多关于如何在使用 ASP.NET 标识时向 ApplicationUser 类(和表)添加属性的文档.但我还没有看到任何关于如何创建一个单独的表的文档,该表的内容通过外键映射到 ApplicationUser 表.

I've seen lots of documentation on how to add properties to the ApplicationUser class (and table) when using ASP.NET identity. But I haven't seen any documentation on how to have a separate table with content that maps to the ApplicationUser table via a foreign key.

我已成功派生出自己的 Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext,现在我自己的UserPreferences"表与 SQL Server 数据库中的各种AspNet*"表和谐共存.但我不清楚获取当前用户 ID 以便将新行写入UserPreferences"表的最佳方法.请注意,该项目是 ASP.NET MVC,但我已添加(并在其中工作)一个 Web API 控制器.

I've successfully derived my own Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext, and now my own "UserPreferences" table coexists in harmony with the various "AspNet*" tables in my SQL Server database. But I'm not clear on the best way to get the current user's ID so as to write a new row to the "UserPreferences" table. Note that the project is ASP.NET MVC, but I've added (and am working inside of) a Web API controller.

我有一个可行的解决方案,即:

I have a working solution, which is:

            var uman = new Microsoft.AspNet.Identity.UserManager<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>(new App1.Data.App1DbContext()));

            var uident = User.Identity;

            var userobject = uman.FindByNameAsync(uident.Name);

            var userid = userobject.Result.Id;
            // (Perform new row creation including the userid we just looked up

考虑到 AspNetUsers 表(由 Identity 框架定义)由以下字段组成:

Consider that the AspNetUsers table (as defined by the Identity framework) consists of these fields:

-id (PK, nvarchar(128) - seems to contain a GUID, not sure why not an autoincrement integer, but I assume there are reasons for this)
-Username (nvarchar(max))
-PasswordHash (nvarchar(max))
-SecurityStamp (nvarchar(max))

我认为 id 字段(不是用户名)是该表中要引用的正确字段.(我在想一个用户可能会改变他/她的用户名,然后其他人可以假设另一个用户的用户名,这就是为什么两个字段都可能存在的原因.)那么,我需要获取当前用户的 ID 以存储在UserPreferences"表,这就是上面代码所做的.但必须进行这种查找似乎效率低下.

I think that the id field (not the username) is the correct field to reference in this table. (I was thinking that a user may change his/her username, and someone else could then assume the other user's username, which is why both fields are there likely.) So then, I need to get the current user's ID for storage in the "UserPreferences" table, which is what the above code does. But it seems inefficient to have to do this lookup.

重要的一点是,在 System.Web.Mvc.Controller 的上下文中,我可以:

An important point is that in the context of a System.Web.Mvc.Controller, I can do:

User.Identity.GetUserId()
(Runtime type of User.Identity: System.Security.Principal.GenericIdentity)

但是在 System.Web.Http.ApiController (Web API) 的上下文中,我不能,因为该方法不存在(User.Identity 的运行时类型:System.Security.Claims.ClaimsIdentity)这就是为什么我必须依靠:

But in the context of a System.Web.Http.ApiController (Web API), I cannot because that method does not exist (runtime type of User.Identity: System.Security.Claims.ClaimsIdentity) which is why I must rely instead on:

User.Identity.Name

并进行额外的查找以将名称转换为 ID.有没有人对如何改进这种情况有任何建议?我是否正在以正确的方式将用户数据写入单独的表?

and do the extra lookup to convert Name to ID. Does anyone have any suggestions for how this can be improved? Am I approaching the task of writing user data to separate tables in the correct way?

推荐答案

您应该能够通过 identity 1.0 RTW 包.

这是身份包的扩展:

namespace Microsoft.AspNet.Identity
{
    public static class IdentityExtensions
    {
        public static string FindFirstValue(this ClaimsIdentity identity, string claimType);
        public static string GetUserId(this IIdentity identity);
        public static string GetUserName(this IIdentity identity);
    }
}

IIdentity 是所有身份类型的基本接口.您可能需要添加使用 Microsoft.AspNet.Identity"才能看到扩展方法.

The IIdentity is the base interface for all identity types. You may need to add "using Microsoft.AspNet.Identity" in order to see the extension method.

顺便说一句:关于为用户添加外部表,为什么不使用 ApplicationUser 并将导航属性添加到 UserPreference 以让 EF 处理它们的关系?那会更容易.

BTW: regarding adding a foreign table for user, why not using ApplicationUser and add navigation property to UserPreference to let EF to handle their relationship? That will be easier.

这篇关于ASP.NET (OWIN) 身份:如何从 Web API 控制器获取用户 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆