summaryrefslogtreecommitdiffstats
path: root/TwoStepsAuthenticator.TestWebsite/Controllers/HomeController.cs
blob: 18b0c29ddad406a2e79a25ee468280413606b40a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using TwoStepsAuthenticator.TestWebsite.Users;

namespace TwoStepsAuthenticator.TestWebsite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        private static readonly UsedCodesManager usedCodesManager = new UsedCodesManager();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string login, string password)
        {
            if (Membership.ValidateUser(login, password))
            {
                var user = WebsiteUserStorage.GetUser(login);
                if (user.DoubleAuthActivated)
                {
                    Session["AuthenticatedUser"] = user;
                    return View("DoubleAuth", user);
                }
                else
                {
                    FormsAuthentication.SetAuthCookie(login, true);
                    return RedirectToAction("Welcome");
                }
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult DoubleAuth(string code)
        {
            WebsiteUser user = (WebsiteUser)Session["AuthenticatedUser"];
            var auth = new TwoStepsAuthenticator.TimeAuthenticator(usedCodeManager: usedCodesManager);
            if (auth.CheckCode(user.DoubleAuthKey, code, user))
            {
                FormsAuthentication.SetAuthCookie(user.Login, true);
                return RedirectToAction("Welcome");
            }

            return RedirectToAction("Index");
        }

        [Authorize]
        public ActionResult Welcome()
        {
            return View() ;
        }

        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
    }
}