传递用户信息到MVC控制器 [英] Passing User Info to MVC Controller
问题描述
我在看不同的方法来实现在我MVC3应用验证。我想用我自己的code做认证 - 类似的东西<一个href=\"http://stackoverflow.com/questions/1438151/is-it-possible-to-create-a-logon-system-with-asp-net-mvc-but-not-use-the-members\">Is它可以创建一个登录系统的ASP.NET MVC,但不使用的MembershipProvider?的(我知道还有其他的方法。)的我想知道,一旦我使用验证用户这些方法,我如何获得用户信息传递给控制器构造之一。的(用户信息,我的意思是用户名或用户ID)。的
I am looking at various methods to implement Authentication in my MVC3 app. I would like to use my own code to do the authentication – something similar to Is it possible to create a Logon System with ASP.NET MVC but not use the MembershipProvider? (I know that there are other methods.) I would like to know once I authenticate a user using one of these methods, how do I get the user information to the Controller constructor. (By user information I mean username or userID).
一个我认为是把这些信息在会话
选项。这工作,但它是很难测试,因为我得到的会话
退出的 Context,它在不存在的测试。
One of the options I considered is putting that info into the Session
. This works, but it is difficult to test since I get the Session
out of the Context
which does not exist during the test.
我想AP preciate了如何将用户信息传递给控制器构造的任何想法。
I would appreciate any ideas for how to pass user info to the controller constructor.
推荐答案
在我原来的职位我一直在寻找通过用户信息到控制器
构造函数。我不想让控制器
取决于的HttpContext
,因为这将使它很难测试。
In my original post I was looking at passing User Info to the Controller
constructor. I did not want the Controller
to depend on HttpContext
, because this would make it difficult to test.
虽然我感谢的空军终于曼作为他的解决方案,我希望下面的替代解决方案将帮助别人。我有一个小项目的(大约十几控制器)的,所以它不是太糟糕了。
While I thank Mystere Man for his solution, I hope the following alternate solution would help someone. I have a small project (about a dozen controllers) so it is not too bad.
我基本上创建我的自定义的ControllerFactory
从 DefaultControllerFactory
继承:
I basically created my custom ControllerFactory
inheriting from DefaultControllerFactory
:
public class MyCustomControllerFactory : DefaultControllerFactory
{
public MyCustomControllerFactory ()
{
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return null;
}
else
{
//Example of User Info - Customer ID
string customerIDStr = requestContext.HttpContext.Session["CustomerID"].ToString();
int customerID = Int32.Parse(customerIDStr);
//Now we create each of the Controllers manually
if (controllerType == typeof(MyFirstController))
{
return new MyFirstController(customerID);
}
else if (controllerType == typeof(MySecondController))
{
return new MySecondController(customerID);
}
//Add/Create Controllers similarly
else //For all normal Controllers i.e. with no Arguments
{
return base.GetControllerInstance(requestContext, controllerType);
}
}
}
}
我然后设置的ControllerFactory
在的Global.asax.cs
的Application_Start()
方法。
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new MyCustomControllerFactory ());
}
P.S。我看着使用DI容器,如Ninject,但我认为他们是为我的当前项目太复杂了。我想看看他们在几个月后当它真正有意义的使用它们。
P.S. I looked into using DI Containers like Ninject, but I think they were too complicated for my current project. I would look at them in a few months when it really makes sense to use them.
这篇关于传递用户信息到MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!