public CustomIdentity(FormsAuthenticationTicket ticket) { _ticket = ticket; var kernel = new StandardKernel(); AccountRepository = new AccountRepository(); kernel.Inject(AccountRepository); }
protected void Application_AuthenticateRequest() { IPrincipal user = HttpContext.Current.User; IAccountRepository accountRepository = new AccountRepository(); if (user != null && user.Identity.IsAuthenticated && user.Identity.AuthenticationType.Equals("Forms")) { var formsIdentity = user.Identity as FormsIdentity; if (formsIdentity != null) { var customIdentity = new CustomIdentity(formsIdentity.Ticket); var customPrincipal = new CustomPrincipal(customIdentity, accountRepository); HttpContext.Current.User = customPrincipal; Thread.CurrentPrincipal = customPrincipal; } } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { // Put into default Role "RegisteredUser". "Administrators" are flagged manually // in the database. _accountRepository.AssignUserToRole(model.UserName, "RegisteredUser"); // Add user data to the custom model database var snippetData = new SnippetCacheDataServiceClient(); var aspnetData = new AccountRepository(snippetData); const string defaultImagePath = "~/Content/images/default_user_avatar.png"; { using ( var avatarStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath(defaultImagePath), FileMode.Open)) { var buffer = new byte[avatarStream.Length]; avatarStream.Read(buffer, 0, (int) avatarStream.Length); var request = new CreateNewUserRequest { LoginName = model.UserName, Email = model.Email, FormsAuthId = aspnetData.GetUserGuid(model.UserName), //AvatarImage = Encoding.UTF8.GetBytes(Url.Content("~/Content/images/default_user_avatar.png")) AvatarImage = buffer }; CreateNewUserResponse response = snippetData.CreateNewUser(request); if (!response.Success) { const string message = "Failure creating new user."; var e = new Exception(response.FailureInformation); Logger.LogError(message, e); throw new AuthenticationException(message, e); } // Create welcome message for user's inbox _managerService.CreateNewUserNotification(new NotificationDTO { NotificationType_Id = 1, User_FormsAuthId = response.FormsAuthId, User_Id = response.Id, Text = "Welcome to SnippetCache!", DateCreated = DateTime.UtcNow }); } FormsAuthentication.SetAuthCookie(model.UserName, false); } ; return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("Error", "Please correct any issues and try again."); } // If we got this far, something failed, redisplay form return View(model); }