[英]How to use the asp.net mvc session in service stack without authentication mechanism
I am working on Asp.Net MVC and Service Stack. 我正在研究Asp.Net MVC和服务堆栈。 I am trying to implement that, make use of asp.net mvc session in service stack service class.
我正在尝试实现这一点,请在服务堆栈服务类中使用asp.net mvc会话。 That means,
就是说
Public ActionResult Index()
{
Session["username"]="xxx";
return View();
}
and from now i need to be able to make use of Session["username"]
in service stack service class, but i am unable to use session in service stack service class. 从现在开始,我需要能够在服务堆栈服务类中使用
Session["username"]
,但是我无法在服务堆栈服务类中使用Session。 HttpContext.Current.Session
throws null exception. HttpContext.Current.Session
引发null异常。 I have referred Social Bootstrap Api service stack sample project, In that they use CustomUserSession
class, that means after authentication they will store data into session like, 我提到了Social Bootstrap Api服务堆栈示例项目,因为它们使用
CustomUserSession
类,这意味着在身份验证之后,他们会将数据存储到会话中,例如,
Plugins.Add(new AuthFeature(
() => new CustomUserSession(), //Use your own typed Custom UserSession type
new IAuthProvider[] {
new CredentialsAuthProvider()
}));
But in my application there is no authentication mechanism, but i need to store some information into the session and use that session in service stack service class. 但是在我的应用程序中没有身份验证机制,但是我需要将一些信息存储到会话中,并在服务堆栈服务类中使用该会话。
To Enable session in service stack without authentication we use, 要在服务堆栈中启用会话而无需身份验证,我们使用
Plugins.Add(new SessionFeature());
so how to use the asp.net mvc session in service stack without authentication. 因此,如何在未经身份验证的情况下在服务堆栈中使用asp.net mvc会话。 Please Guide Me.
请指导我。
An important distinction to know about, is that ServiceStack Sessions are completely independent and are in no way related (except by name) from ASP.NET sessions. 要知道的一个重要区别是ServiceStack会话是完全独立的,并且与ASP.NET会话没有任何关系(名称除外)。 For the most part Sessions in ServiceStack are simply blobs stored in the registered Cache provider and referenced with the SessionId Cookies that are sent with each HTTP Request.
在大多数情况下,ServiceStack中的会话只是存储在已注册的缓存提供程序中的blob ,并由随每个HTTP请求发送的SessionId Cookies引用。
The easiest way to access ServiceStack sessions in ASP.NET MVC is to extend the ServiceStackController and use the SessionAs<T>()
method to access your typed session, eg: 在ASP.NET MVC中访问ServiceStack会话的最简单方法是扩展ServiceStackController并使用
SessionAs<T>()
方法访问键入的会话,例如:
public class MyMvcController : ServiceStackController
{
public ActionResult Index()
{
MyUserSession myServiceStackSession = base.SessionAs<MyUserSession>();
return View();
}
}
This makes use of the registered ICacheClient
provider which is injected in the base.Cache
property. 这利用了注入到
base.Cache
属性中的已注册ICacheClient
提供程序。 You can use ServiceStack's IOC to autowire ASP.NET MVC controllers with dependencies registered in ServiceStack's IOC by setting MVC's SetControllerFactory()
, eg: 您可以通过设置MVC的
SetControllerFactory()
来使用ServiceStack的IOC自动连接具有在ServiceStack的IOC中注册的依赖项的ASP.NET MVC控制器,例如:
public override void Configure(Funq.Container container)
{
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(
new FunqControllerFactory(container));
}
Otherwise if you want to use a different IOC for ASP.NET MVC Controllers and ServiceStack you would need to register the same ICacheClient Provider that's registered in ServiceStack's IOC, in your MVC IOC. 否则,如果要为ASP.NET MVC控制器和ServiceStack使用不同的IOC,则需要在MVC IOC中注册与ServiceStack的IOC中注册的相同的ICacheClient Provider 。 If no CacheClient is registered, ServiceStack uses the
MemoryCacheClient
by default. 如果未注册CacheClient,则ServiceStack默认使用
MemoryCacheClient
。
See the wiki for more info about integrating ServiceStack with ASP.NET MVC . 有关将ServiceStack与ASP.NET MVC集成的更多信息,请参见Wiki。
When hosted on ASP.NET you can access the underlying ASP.NET request with: 当托管在ASP.NET上时,您可以使用以下命令访问基础的ASP.NET请求:
public class MyServices : Service
{
public object Any(MyRequest request)
{
var aspReq = base.Request.OriginalRequest as HttpRequestBase;
if (aspReq != null)
{
var value = aspReq.RequestContext.HttpContext.Session["key"];
}
//or if you prefer via the ASP.NET Singleton:
var value = HttpContext.Current.Session["key"];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.