Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
I am working on one MVC application, where there is separate WEB API project also. Now I want to implement update email functionality based on OTP verification from mobile via WEB API. Web API is stateless but still, I want to implement SESSION in my code so that I can access OTP value in subsequence request and validate it. Currently, I am able to store OTP in session in the first request but in next request Session is null and I can not access stored OTP value.

I do not want database trip to store and retrieve the OTP.

public HttpResponseMessage SendOtpOnMobile([FromBody]OtpOnMobileAPIRequest objOtpOnMobileAPIRequest)
{            
    otpValue = CommonUtility.GenerateRandomOTP(4);
    HttpContext.Current.Session["otpGeneratedValue"] = otpValue;
    //Send OTP logic and response code
    ....
}

public HttpResponseMessage ValidateOtpFromMobile([FromBody]ValidateOtpFromMobileAPIRequest objValidateOtpFromMobileAPIRequest)
{
    var otpGeneratedValue = HttpContext.Current.Session["otpGeneratedValue"];
    if(objValidateOtpFromMobileAPIRequest.OTP == otpGeneratedValue)
    {
        //success
    }
    ....
}


What I have tried:

ASP.NET Web API session or something?
Ask


Accessing Session Using ASP.NET Web API
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900