PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/AnotherStartup/App_Code/Account/ChangePassword.cs

#
C# | 80 lines | 67 code | 10 blank | 3 comment | 8 complexity | 3f2714b997bd0547887b177b1a45c7c8 MD5 | raw file
 1using System.Web.WebPages;
 2using WebMatrix.WebData;
 3
 4
 5namespace Account
 6{
 7    /// <summary>
 8    /// Summary description for ChangePassword
 9    /// </summary>
10    public static class ChangePassword
11    {
12        public class Model
13        {
14            public string CurrentPassword { get; set; }
15            public string NewPassword { get; set; }
16            public string ConfirmPassword { get; set; }
17        }
18
19        public class Error
20        {
21            public ErrorMessage CurrentPassword { get; set; }
22            public ErrorMessage NewPassword { get; set; }
23            public ErrorMessage ConfirmPassword { get; set; }
24            public ErrorMessage Provider { get; set; }
25
26            public Error()
27            {
28                CurrentPassword = ErrorMessage.Nil;
29                NewPassword = ErrorMessage.Nil;
30                ConfirmPassword = ErrorMessage.Nil;
31                Provider = ErrorMessage.Nil;
32            }
33        }
34
35        public static void Init(WebPageBase web)
36        {
37            Model model = web.Page.Model = new Model();
38            Error error = web.Page.Errors = new Error();
39
40            web.Page.Success = false;
41            
42            model.CurrentPassword = web.Request["currentPassword"];
43            model.NewPassword = web.Request["newPassword"];
44            model.ConfirmPassword = web.Request["confirmPassword"];
45
46            if (web.IsPost)
47            {
48                if (model.CurrentPassword.IsEmpty())
49                {
50                    error.CurrentPassword = web.Error("Please enter your current password.");
51                }
52                if (model.NewPassword.IsEmpty())
53                {
54                    error.NewPassword = web.Error("Please enter a new password.");
55                }
56                if (model.ConfirmPassword.IsEmpty())
57                {
58                    error.ConfirmPassword = web.Error("Please confirm your new password.");
59                }
60                if (model.ConfirmPassword != model.NewPassword)
61                {
62                    error.ConfirmPassword = web.Error("The password confirmation does not match the new password.");
63                }
64
65                if (web.Page.IsValid)
66                {
67                    if (WebSecurity.ChangePassword(WebSecurity.CurrentUserName, model.CurrentPassword, model.NewPassword))
68                    {
69                        web.Page.Success = true;
70                        
71                    }
72                    else
73                    {
74                        error.Provider = web.Error("An error occurred when attempting to change the password. Please contact the site owner.");
75                    }
76                }
77            }
78        }
79    }
80}