我刚开始在ASP.NET AJAX中使用WCF服务.我从Javascript实例化我的WCF服务,然后将字符串变量作为参数传递给我的WCF服务方法(带有OperationContract签名).然后我返回一个.NET对象(使用DataContract定义),该对象绑定到我的自定义Javascript类.我根据登录到我的网络会话的用户进行身份验证时遇到问题.但是,WCF Web服务是一个完全不同的服务,没有HttpContext.Current对象的上下文.访问该对象的最安全的方法是什么?
我有一个基于MVC4的Windows身份验证的Intranet应用程序.
当我在控制器中需要WindowsIdentity进行授权时,我只是使用
HttpContext.User.Identity
Run Code Online (Sandbox Code Playgroud)
现在我想使用新的Asp.Net WebAPI进行一些Ajax调用.
有没有办法以与MVC控制器相同的方式获取WindowsIdenty对象?
如何从自托管的MVC WebAPI访问查询字符串?
使用NRE调用以下失败,因为Current为空(aka.null)
System.Web.HttpContext.Current.Request["myQuery"]
Run Code Online (Sandbox Code Playgroud)
我需要访问控制器外部的当前上下文,因为我想通过控制我的对象实例化.DI.例如.
container
.RegisterType<MyObject>(
new InjectionFactory(
uc =>
new MyObject(
System.Web.HttpContext.Current.Request["myParam"]) //This failed.
));
Run Code Online (Sandbox Code Playgroud)
container.Resolve<MyObject>()由于上述NRE,从ControllerApi代码内部调用失败.
我浏览了各种帖子并尝试了几件事但我无法让事情正常进行.我不觉得应该那么难,但这就是我得到的.我只想将文件发布到我的服务器以进行测试.它应该像我从<input type="file"/>网页上的标准上传它一样工作.
这是我想要测试的实际处理程序:
[Route("MyAction")]
[HttpPost]
public IHttpActionResult MyAction()
{
try
{
var request = HttpContext.Current.Request;
if (request.Files.Count == 1)
{
var postedFile = request.Files[0];
if (request.Files[0].FileName.EndsWith(".zip"))
{
// unzip the file
// do stuff
}
}
}
catch
{
return new ValidationError("An error has occurred and has been logged");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试:
public void SetupServer()
{
server = TestServer.Create(app =>
{
var startup = new Startup();
startup.ConfigureOAuth(app);
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
});
}
[Test]
public …Run Code Online (Sandbox Code Playgroud)