खोज…


परिचय

ASP.NET कोर 1.0 में सत्र का उपयोग करना

सेशन को संभालने का मूल उदाहरण

1) सबसे पहले, project.json में निर्भरता जोड़ें - "Microsoft.AspNetCore.Session": "1.1.0",

2) में startup.cs और जोड़ने AddSession() और AddDistributedMemoryCache() के लिए लाइनों ConfigureServices इस- की तरह

services.AddDistributedMemoryCache(); //This way ASP.NET Core will use a Memory Cache to store session variables
services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromDays(1); // It depends on user requirements.
            options.CookieName = ".My.Session"; // Give a cookie name for session which will be visible in request payloads.
        });

3) इस तरह स्टार्टअप के कॉन्फ़िगर विधि में UseSession() कॉल UseSession()

app.UseSession(); //make sure add this line before UseMvc()

4) नियंत्रक में, सत्र वस्तु का उपयोग इस तरह किया जा सकता है-

using Microsoft.AspNetCore.Http;

public class HomeController : Controller
{
    public IActionResult Index()
    { 
        HttpContext.Session.SetString("SessionVariable1", "Testing123");
        return View();
    }

    public IActionResult About()
    {
        ViewBag.Message = HttpContext.Session.GetString("SessionVariable1");

        return View();
    }
}
  1. यदि आप cors पॉलिसी का उपयोग कर रहे हैं तो कभी-कभी यह सक्षम करने के बाद त्रुटियां दे सकता है
    AllowCredentials हेडर को सक्षम करने और उपयोग करने के बारे में हेडर के बारे में सत्र
    WithOrigins AllowAllOrigins के बजाय शीर्षक।


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow