简体   繁体   English

如何在没有身份验证机制的情况下在服务堆栈中使用asp.net mvc会话

[英]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。

Accessing ASP.NET Request and Session in ServiceStack Services 在ServiceStack Services中访问ASP.NET请求和会话

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.

相关问题 如何在ASP.Net MVC应用程序中使用来自WCF身份验证服务的身份验证cookie - How to use authentication cookie from WCF Authentication Service in an ASP.Net MVC application 如何在没有ASP.NET和MVC的情况下在OWIN中创建身份验证 - How to create authentication in owin without ASP.NET and without MVC 如何为asp.net mvc和Web API实施相同的身份验证机制 - How to implement the same authentication mechanism for both the asp.net mvc and web API 如何使用 ASP.NET MVC 3 和 Stack Overflow 的 Markdown - How to use ASP.NET MVC 3 and Stack Overflow's Markdown 如何使用基本身份验证来访问从Windows服务使用Windows身份验证的Asp.Net MVC 4 Intranet Web API方法? - How to use Basic Authentication to access Asp.Net MVC 4 Intranet Web API method which uses Windows Authentication from a Windows Service? 如何在Asp.net MVC 4应用程序中使用身份验证和授权? - How to use Authentication and Authorization in Asp.net MVC 4 application? 如何在ASP.Net MVC控制器中正确使用ServiceStack身份验证 - How to use ServiceStack authentication correctly in ASP.Net MVC controller 如何在asp.net mvc 4中的apicontroller中使用session - How to use session in apicontroller in asp.net mvc 4 如何在Asp.Net MVC3中使用会话 - How to use Session in Asp.Net MVC3 如何使用会话和类asp.net mvc 6进行调用 - How to use session and call from class asp.net mvc 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM