繁体   English   中英

新ASP.NET Web API的主体

[英]Principal with new ASP.NET Web API

前一段时间,我使用asp.net Web-api beta构建了API。 我在某处读到,当托管在IIS中时,web-api从运行其的Web应用程序继承了Identity。 我的api也正在网站上运行。 使用beta二进制文件时,当我登录该站点然后移至http://localhost:4343/webui/api/values ,它将为我返回已登录用户的正确值。 这是值的Get方法控制器。

public IQueryable<string> Get()
        {
            var pr = Request.GetUserPrincipal();
            var username = Request.GetUserPrincipal().Identity.Name; //Null reference exception after installing vs 2012 here. Identity is null even though I am logged in 
            var values = GetUserValues(username);
            return values.AsQueryable();
        }

它过去可以与Web-api的Beta版一起正常使用,但是在vs2012的最终版本中会引发空引用异常。 我已将网站转换为使用.net 4.5。 知道这里发生了什么吗?

ApiController现在具有一个称为“ User”的属性,它是IPrincipal。

var pr = User.Identity.Name;

在内部,用户属性调用tugberk提到的Thread.CurrentPrincipal。

HttpRequestMessage没有任何方法或扩展名,例如GetUserPrincipal 我不确定beta是否具有该功能。 从您的问题中了解到,您的应用仍在Beta版上运行,这完全是灾难IMO。

在安装VS 2012时,Web API程序集已在计算机上GACed。 因此,您极有可能在那里发生冲突。

另一方面,您可以通过以下命令获取身份:

var principal = Thread.CurrentPrincipal;

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM