I am trying to integrate the google authenticator app with my asp.net core web app but my Session in my home controller keeps giving errors. I'm looking for possible fixes.
using Google.Authenticator; using Microsoft.AspNetCore.Mvc;
namespace QAService.Controllers { public class HomeController : Controller { public ActionResult Login() { return View(); }
[HttpPost]
public ActionResult Login(FormCollection fc)
{
if (fc["username"] == "Admin" && fc["password"] == "Admin123")
{
Session["sampleid"] = fc["username"];
return RedirectToAction("VerifyAuth");
}
else
{
return View();
}
}
public ActionResult VerifyAuth()
{
if (Session["sampleid"] != null)
{
return RedirectToAction("Login");
}
else
{
return View();
}
}
string key = "test123!@@)(*";
[HttpPost]
public ActionResult VerifyAuth(FormCollection fc)
{
if (Session["sampleid"] == null)
{
return RedirectToAction("Admin_Login");
}
var token = fc["passcode"];
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
string UserUniqueKey = (Convert.ToString(Session["sampleid"]) + key);
bool isValid = tfa.ValidateTwoFactorPIN(UserUniqueKey, token);
if (isValid)
{
Session["id"] = Convert.ToString(Session["sampleid"]);
return RedirectToAction("Myprofile");
}
return RedirectToAction("Login");
}
public ActionResult AdminQR()
{
if (Session["sampleid"] != null)
{
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
string UserUniqueKey = (Convert.ToString(Session["sampleid"]) + key);
Session["UserUniqueKey"] = UserUniqueKey;
var setupInfo = tfa.GenerateSetupCode("Google Auth Test", Convert.ToString(Session["sampleid"]), UserUniqueKey, 300, 300);
ViewBag.BarcodeImageUrl = setupInfo.QrCodeSetupImageUrl;
ViewBag.SetupCode = setupInfo.ManualEntryKey;
return View();
}
else
{
return RedirectToAction("Login");
}
}
public ActionResult Myprofile()
{
if (Session["id"] != null)
{
return View();
}
else
{
return RedirectToAction("Login");
}
}
}
}