PageRenderTime 72ms CodeModel.GetById 30ms app.highlight 5ms RepoModel.GetById 25ms app.codeStats 0ms

/AnotherStartup/App_Code/Account/Login.cs

#
C# | 88 lines | 65 code | 11 blank | 12 comment | 9 complexity | 7221006f50f046a2ba1bd9636aa9a830 MD5 | raw file
 1using System;
 2using System.Web.WebPages;
 3using WebMatrix.WebData;
 4
 5/// <summary>
 6/// Summary description for Login
 7/// </summary>
 8/// 
 9namespace Account
10{
11    public static class Login
12    {
13        public class Model
14        {
15            public string Username { get; set; }
16            public string Password { get; set; }
17            public bool RememberMe { get; set; }
18        }
19
20        public class Errors
21        {
22            public ErrorMessage Username { get; set; }
23            public ErrorMessage Password { get; set; }
24
25            public Errors()
26            {
27                Username = ErrorMessage.Nil;
28                Password = ErrorMessage.Nil;
29            }
30        }
31
32        public static void Init(WebPageBase web)
33        {
34            // Initialize model properties      
35            Model model = web.Page.Model = new Model();
36
37            //  Initialize validation properties
38            Errors errors = web.Page.Errors = new Errors();
39
40            // If this is a POST request, validate and process data
41            if (web.IsPost)
42            {
43                model.Username = web.Request.Form["username"];
44                model.Password = web.Request.Form["password"];
45                model.RememberMe = web.Request.Form["rememberMe"].AsBool();
46
47                // Validate the user's username
48                if (model.Username.IsEmpty())
49                {
50                    errors.Username = web.Error("You must specify a username.");                    
51                }
52
53                // Validate the user's password
54                if (model.Password.IsEmpty())
55                {
56                    errors.Password = web.Error("You must specify a password.");                    
57                }
58
59                // Confirm there are no validation errors
60                if (web.Page.IsValid)
61                {
62                    if (WebSecurity.UserExists(model.Username) && WebSecurity.GetPasswordFailuresSinceLastSuccess(model.Username) > 4 && WebSecurity.GetLastPasswordFailureDate(model.Username).AddSeconds(60) > DateTime.UtcNow)
63                    {
64                        web.Response.Redirect("~/account/AccountLockedOut");
65                        return;
66                    }
67
68                    // Attempt to login to the Security object using provided creds
69                    if (WebSecurity.Login(model.Username, model.Password, model.RememberMe))
70                    {
71                        var returnUrl = web.Request.QueryString["ReturnUrl"];
72                        if (returnUrl.IsEmpty())
73                        {
74                            web.Response.Redirect("~/");
75                        }
76                        else
77                        {
78                            web.Context.RedirectLocal(returnUrl);
79                        }
80                    }
81
82                    // If we arrived here, the login failed; convey that to the user
83                    web.Page.IsValid = false;
84                }
85            }
86        }
87    }
88}