为什么我无法使用会话存储来存储电子邮件? (正在使用 ASP.NET Core MVC)

问题描述 投票:0回答:1

我正在尝试在下面的操作控制器中设置会话字符串。我在哪里设置电子邮件。

[HttpPost]
public IActionResult LoginFunc(Login model)
{
    try
    {
        bool employeeIdExists = _DAL.EmployeeIdExists(model.RMEmployeeId);

        if (!employeeIdExists)
        {
            ViewBag.ErrEmployeeDoesntExists = "This Employee Id doesn't exist.";
            return View("~/Views/Home/Login.cshtml");
        }
        else
        {
            _employeeEmailId = _DAL.GetEmailByEmployeeId(model.RMEmployeeId);
            string otp = GenerateOtp(6);

            //TempData["EmployeeEmailId"] = employeeEmailId;
            //TempData["Otp"] = otp;

            //HttpContext.Session.SetString("Otp", otp);

            //Using smtp here                    
            Login_DAL.SendOtpByEmail(otp, smtpHost, smtpPort, cMailId, cMailIDPassword, fromEmailId, configuration, _employeeEmailId);

            DateTime expiryDateTime = DateTime.Now.AddMinutes(30);
            _DAL.AddOtpData(_employeeEmailId, otp, expiryDateTime);

            //SendOtpByEmail(employeeEmailId, otp, smtpHost, smtpPort, cMailId, cMailIDPassword, fromEmailId);
            ViewBag.DisplayOtpModal = true;

            HttpContext.Session.SetString("EmployeeEmail", employeeEmailId);

            // return View("~/Views/Home/OtpPage.cshtml");
            return RedirectToAction("VerifyOtpPage");
        }
    }
    catch (Exception ex)
    {
        TempData["errorMessage"] = ex.Message;
        return View("~/Views/Home/Login.cshtml");
    }
}

在上面的代码中,我没有收到任何错误,但是当我的调试器点击会话行时,我被重定向到登录页面。而且我的代码不会进一步运行来显示 otp 验证页面。

我正在尝试在下面的代码中获取会话值:

[HttpPost]
public IActionResult VerifyOtp(VerifyOtp enteredOtp)
{
    try
    {
        if (string.IsNullOrEmpty(enteredOtp.Otp))
        {
            ModelState.AddModelError("Otp", "Please enter the Otp.");
            return View("~/Views/Home/OtpPage.cshtml");
        }

        string employeeEmailId = HttpContext.Session.GetString("EmployeeEmail");
                
        bool verifiedOtp = _DAL.VerifyOtp(enteredOtp.Otp, employeeEmailId);

        if (verifiedOtp)
        {
            // return View("~/Views/Home/HomeIndex.cshtml");
            return RedirectToAction("HomeIndex", "Home");
            // return View();
        }
        else
        {
            ViewBag.DisplayOtpModal = true;
            ViewBag.IncorrectOtp = "Invalid or expired OTP.";

            return View("~/Views/Home/OtpPage.cshtml");
        }
    }
    catch (Exception ex)
    {
        TempData["errorMessage"] = ex.Message;
        return View("~/Views/Home/OtpPage.cshtml");
    }
}

这是我的

startup.cs
文件:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.AspNetCore.Http;

namespace WebApplicationFinal
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // Configure your services here

            services.AddDistributedMemoryCache();
            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromMinutes(30);
                options.Cookie.HttpOnly = true;
                options.Cookie.IsEssential = true;
            });

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // Configure your middleware here

            app.UseRouting();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSession(); // This should be placed after UseRouting and before UseEndpoints

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=MainPageView}");
            });
        }
    }
} 
asp.net-core-mvc session-state asp.net-core-mvc-2.0 asp.net-session
1个回答
0
投票

我在使用asp.net core 6时使用了asp.net core 5方法。在.net 6中没有startup.cs文件,配置写在program.cs文件中。

© www.soinside.com 2019 - 2024. All rights reserved.