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

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

问题描述

(使用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,现在我自己的用户preferences表中和谐共存,在我的SQL Server数据库中的各种ASPNET *表。但我不是来获取当前用户的ID,以写一个新行到用户preferences表的最佳途径清晰。请注意,该项目是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表(由身份框架中定义)包括这些字段:

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存储在用户preferences表,这是上面的code做了什么。但似乎低效不得不这样做查找。

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(网页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?

感谢您的任何建议...

Thanks for any suggestions...

-Ben

推荐答案

您应该能够获得用户ID通过相同的扩展方法都MVC控制器和Web API控制器上的身份1.0 RTW包

You should be able to get user id on both MVC controller and web api controller by same extension method in identity 1.0 RTW package.

下面是身份包扩展名:

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.

BTW:关于增加一个外部表用户,为什么不使用ApplicationUser和导航属性添加到用户preference让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控制器获得用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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