添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am building a login system to improve my coding skills in .NET. Currently i am building the project in Azure Functions. The Azure Functions version i am using runs on .NET core 3.1.

The problem that i have is that i am unable to find a sessions and cookies class/library in the .NET Core framework. The classes that exists are part of ASP.NET Core. ASP.NET Core is not included in Azure Functions. I have already tried to search for libraries that allow this functionality, however i was not successful.

How can i create sessions and cookies in Azure Functions?

Thank you in advance.

Firstly, the primary use cases for Azure Functions are stateless, and sessions are a bit against this principle.

If you insist on this, you could use HttpRequestMessage to implement it, the below is my test code.

    public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, 
            ILogger log)
            log.LogInformation("C# HTTP trigger function processed a request.");
            var resp = new HttpResponseMessage();
            var cookie = new CookieHeaderValue("session-id", "12345");
            cookie.Expires = DateTimeOffset.Now.AddDays(1);
            cookie.Domain = req.RequestUri.Host;
            cookie.Path = "/";
            resp.Headers.AddCookies(new CookieHeaderValue[] { cookie });
            return resp;

And below is my result picture, help this is what you want if you still have other problem please feel free to let me know.

Thank you George. The solution provided works! What can be used to generate secure sessions values? Can i use GUID's? – Raaz May 7, 2020 at 8:33 @Raaz,This name and value are customized, so yes you could. And if this could help you, you could accept it as the answer. – George Chen May 7, 2020 at 8:36 I've found multiple examples of setting a cookie, but not a single one of reading a cookie. Can you actually access them in future requests? – Joe Jul 21, 2022 at 13:14

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.