Guest User

Untitled

a guest
Aug 7th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. ASP.NET MVC 3 inheriting Membership userId
  2. public class Profile
  3. {
  4. [Key]
  5. public Guid ProfileId { get; set; }
  6. public string LastName { get; set; }
  7. public string FirstName { get; set; }
  8.  
  9. public virtual MembershipUser User
  10. {
  11. get { return Membership.GetUser(ProfileId); }
  12. }
  13.  
  14. public string FullName
  15. {
  16. get { return LastName + ", " + FirstName; }
  17. }
  18. }
  19.  
  20. public class AuthenticationService : IAuthenticationService
  21. {
  22. private readonly IConfigHelper _configHelper;
  23. private readonly ISession _session;
  24.  
  25. public AuthenticationService(IConfigHelper configHelper, ISession session)
  26. {
  27. _configHelper = configHelper;
  28. _session = session;
  29. }
  30.  
  31. public bool IsValidLogin(string email, string password)
  32. {
  33. CheckLocked(email);
  34. return Membership.ValidateUser(email, password);
  35. }
  36.  
  37. public void SignIn(string email, bool createPersistentCookie)
  38. {
  39. if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
  40. FormsAuthentication.SetAuthCookie(email, createPersistentCookie);
  41. }
  42.  
  43. public void SignOut()
  44. {
  45. FormsAuthentication.SignOut();
  46. }
  47.  
  48. public User GetLoggedUser()
  49. {
  50. var email = GetLoggedInUserName();
  51.  
  52. if (IsMember())
  53. return _session.Single<Member>(x => x.Email == email);
  54.  
  55. return _session.Single<DelegateMember>(x => x.Email == email);
  56. }
  57.  
  58. public string GetLoggedInUserName()
  59. {
  60. return Membership.GetUser() != null ? Membership.GetUser().UserName : string.Empty;
  61. }
  62.  
  63. public MembershipCreateStatus RegisterUser(string email, string password, string role)
  64. {
  65. MembershipCreateStatus status;
  66. //On doit laisser Guid.NewGuid().ToString() sinon ça ne passe pas
  67. Membership.CreateUser(email, password, email, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, out status);
  68.  
  69. if (status == MembershipCreateStatus.Success)
  70. {
  71. Roles.AddUserToRole(email, role);
  72. }
  73. return status;
  74. }
  75.  
  76. public MembershipUserCollection GetAllUsers()
  77. {
  78. return Membership.GetAllUsers();
  79. }
  80.  
  81. public string GeneratePassword()
  82. {
  83. var alphaCaps = "QWERTYUIOPASDFGHJKLZXCVBNM";
  84. var alphaLow = "qwertyuiopasdfghjklzxcvbnm";
  85. var numerics = "1234567890";
  86. var special = "@#$";
  87. var allChars = alphaCaps + alphaLow + numerics + special;
  88. var r = new Random();
  89. var generatedPassword = "";
  90. for (int i = 0; i < MinPasswordLength - 1; i++)
  91. {
  92. double rand = r.NextDouble();
  93. if (i == 0)
  94. {
  95. //First character is an upper case alphabet
  96. generatedPassword += alphaCaps.ToCharArray()[(int)Math.Floor(rand * alphaCaps.Length)];
  97. //Next one is numeric
  98. rand = r.NextDouble();
  99. generatedPassword += numerics.ToCharArray()[(int) Math.Floor(rand*numerics.Length)];
  100. }
  101. else
  102. {
  103. generatedPassword += allChars.ToCharArray()[(int)Math.Floor(rand * allChars.Length)];
  104. }
  105. }
  106. return generatedPassword;
  107. }
  108.  
  109. public int MinPasswordLength
  110. {
  111. get
  112. {
  113. return Membership.Provider.MinRequiredPasswordLength;
  114. }
  115. }
  116.  
  117. public string AdminRole
  118. {
  119. get { return "admin"; }
  120. }
  121.  
  122. public string MemberRole
  123. {
  124. get { return "member"; }
  125. }
  126.  
  127. public string DelegateRole
  128. {
  129. get { return "delegate"; }
  130. }
  131.  
  132. public string AgentRole
  133. {
  134. get { return "agent"; }
  135. }
  136.  
  137. public bool Delete(string email)
  138. {
  139. return Membership.DeleteUser(email);
  140. }
  141.  
  142. public bool IsAdmin()
  143. {
  144. return Roles.IsUserInRole(AdminRole);
  145. }
  146.  
  147. public bool IsMember()
  148. {
  149. return Roles.IsUserInRole(MemberRole);
  150. }
  151.  
  152. public bool IsDelegate()
  153. {
  154. return Roles.IsUserInRole(DelegateRole);
  155. }
  156.  
  157. public bool IsAgent()
  158. {
  159. return Roles.IsUserInRole(AgentRole);
  160. }
  161.  
  162. public bool ChangePassword(string email, string oldPassword, string newPassword)
  163. {
  164. if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
  165. if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword");
  166. if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword");
  167.  
  168. // The underlying ChangePassword() will throw an exception rather
  169. // than return false in certain failure scenarios.
  170. try
  171. {
  172. var currentUser = Membership.Provider.GetUser(email, true);
  173. return currentUser.ChangePassword(oldPassword, newPassword);
  174. }
  175. catch (ArgumentException)
  176. {
  177. return false;
  178. }
  179. catch (MembershipPasswordException)
  180. {
  181. return false;
  182. }
  183. }
  184.  
  185. public string ResetPassword(string email)
  186. {
  187. if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
  188. Unlock(email);
  189. var currentUser = Membership.Provider.GetUser(email, false);
  190. return currentUser.ResetPassword();
  191. }
  192.  
  193. public bool CheckLocked(string email)
  194. {
  195. if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
  196. var currentUser = Membership.Provider.GetUser(email, false);
  197. if (currentUser == null) return false;
  198. if (!currentUser.IsLockedOut) return false;
  199. if (currentUser.LastLockoutDate.AddMinutes(30) < DateTime.Now)
  200. {
  201. currentUser.UnlockUser();
  202. return false;
  203. }
  204. return true;
  205. }
  206.  
  207. public bool Unlock(string email)
  208. {
  209. if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email");
  210. var currentUser = Membership.Provider.GetUser(email, false);
  211. if (currentUser == null) return false;
  212. currentUser.UnlockUser();
  213. return true;
  214.  
  215. }
  216. }
Add Comment
Please, Sign In to add comment