在ASP.NET中传递的用户名与Web API方法MVC 4 [英] Passing username to Web Api methods in ASP.NET MVC 4
问题描述
我使用ASP.NET MVC 4与网页API
我有以下ApiController。
公共ProductsController类:ApiController
{
公开名单<产品与GT;的GetProducts()
{
返回_productService.GetAllProducts();
} 公开名单<产品与GT; GetProductsFromId(用户名字符串)
{
返回_productService.GetProductsFromUsername(用户名);
}
}
现在,如果你看到第二个动作 GetProductsFromId(用户名字符串)
这里我需要通过用户名而早期(即从MVC 3升级前)我是用 User.Identity.Username
来获取用户名。
我应该如何在这样的情况下处理此
如何通过用户名?有什么我可以做的。
另外,我不想使用 User.Identity.Username
的 GetProductFromId(用户名字符串)
方法里面它会打败使用Web API的全部目的并不会太测试的
请指导我帮助我在此。谢谢
另外,我不想使用User.Identity.Username内
因为它会打败整个GetProductFromId(用户名字符串)方法
目的使用Web API并不会检验的太。
块引用>这正是你应该使用什么:
[授权]
公开名单<产品与GT; GetProductsFromId()
{
字符串的用户名= User.Identity.Name;
返回_productService.GetProductsFromUsername(用户名);
}注意
[授权]
属性。根据不同的授权方案您使用的是User.Identity
将不同的填充。例如,如果您已启用窗体身份验证,则用户名会明显来自于客户端需要通过调用动作时,窗体身份验证cookie。如果您正在使用基本身份验证,而不是形式的认证,你也可以写,例如自定义处理程序。我这里写了一个例子。这并不击败单元测试的任何目的。这种方法是完全正常的单元测试。在<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.user%28v=vs.108%29.aspx\">User财产<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller%28v=vs.108%29.aspx\">ApiController是
的IPrincipal
这可能在单元测试来嘲笑平凡。例如用起订量://安排
VAR SUT =新的ProductsController();
VAR用户=新的模拟&LT;&的IPrincipal GT;();
VAR身份=新的模拟&LT;&IIdentity的GT;();
user.Setup(X =&GT; x.Identity).Returns(identity.Object);
identity.Setup(X =&GT; x.Name).Returns(约翰);
= Thread.CurrentPrincipal中user.Object;//行为
VAR实际= sut.GetProductsFromId();//断言
...I am using ASP.NET MVC 4 with Web Api
I have the following ApiController.
public class ProductsController : ApiController { public List<Product> GetProducts() { return _productService.GetAllProducts(); } public List<Product> GetProductsFromId(string username) { return _productService.GetProductsFromUsername(username); } }
Now if you see the second Action
GetProductsFromId(string username)
here I need to pass username which earlier (i.e before upgrading from MVC 3) I was usingUser.Identity.Username
to get the username.How should I handle this in such a scenario.
How do I pass the username ? Is there something I could do.
Also the I dont want to use
User.Identity.Username
inside theGetProductFromId(string username)
method as it will defeat the entire purpose of using web api and will not be testable too.Please guide me help me on this. Thanks
解决方案Also the I dont want to use User.Identity.Username inside the GetProductFromId(string username) method as it will defeat the entire purpose of using web api and will not be testable too.
That's exactly what you should use:
[Authorize] public List<Product> GetProductsFromId() { string username = User.Identity.Name; return _productService.GetProductsFromUsername(username); }
Notice the
[Authorize]
attribute. Depending on the authorization scheme you are using theUser.Identity
will be populated differently. For example if you have enabled forms authentication then the username will obviously come from the forms authentication cookie that the client need to pass when invoking the action. You could also write a custom handler for example if you are using basic authentication instead of forms authentication. I wrote an example here.This doesn't defeat any purpose of unit testing. This method is perfectly fine unit testable. The User property of the ApiController is an
IPrincipal
which could be trivially mocked in a unit test. For example with Moq:// arrange var sut = new ProductsController(); var user = new Mock<IPrincipal>(); var identity = new Mock<IIdentity>(); user.Setup(x => x.Identity).Returns(identity.Object); identity.Setup(x => x.Name).Returns("john"); Thread.CurrentPrincipal = user.Object; // act var actual = sut.GetProductsFromId(); // assert ...
这篇关于在ASP.NET中传递的用户名与Web API方法MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!