Example #1
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = UserName.Text;

            var             manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext()));
            ApplicationUser u       = new ApplicationUser(userName)
            {
                UserName  = userName,
                FirstName = this.TextBoxFirstName.Text,
                LastName  = this.TextBoxLastName.Text,
                Email     = this.TextBoxEmail.Text,
                JoinDate  = DateTime.Now,
            };
            var context = new AcademyDbContext();

            string fileName = string.Empty;

            var fileUpload = this.FileUploadAvatar;

            if (fileUpload.HasFile)
            {
                if (fileUpload.PostedFile.ContentLength < 102400 &&
                    (fileUpload.PostedFile.ContentType == PngImageFormat ||
                     fileUpload.PostedFile.ContentType == JpegImageFormat ||
                     fileUpload.PostedFile.ContentType == GifImageFormat))
                {
                    fileName = userName.Replace("<", string.Empty).Replace(">", string.Empty) + GetAvatarExtension(FileUploadAvatar.PostedFile.FileName);
                    fileUpload.SaveAs(Server.MapPath(MainPath) + fileName);
                    u.AvatarPath = MainPath + fileName;
                }
                else
                {
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    ErrorSuccessNotifier.AddErrorMessage("The uploaded avatar exceeds 100KB or is in a wrong format.");
                    Response.Redirect(Request.RawUrl, false);
                    return;
                }
            }
            else
            {
                u.AvatarPath = DefaultImagePath;
            }

            IdentityResult result = manager.Users.CreateLocalUser(u, Password.Text);

            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                ErrorSuccessNotifier.ShowAfterRedirect = true;
                ErrorSuccessNotifier.AddSuccessMessage("Registration completed successfully.");
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage(result.Errors.FirstOrDefault());
            }
        }
 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext())).Authentication;
         IdentityResult         result  = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else
         {
             FailureText.Text     = result.Errors.FirstOrDefault();
             ErrorMessage.Visible = true;
         }
     }
 }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var user = new ApplicationUser(userName.Text);
            IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext())).Authentication;
            IdentityResult         result  = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user);

            if (result.Success)
            {
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                AddErrors(result);
                return;
            }
        }
 protected void Page_Load()
 {
     // Process the result from an auth provider in the request
     ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request);
     if (String.IsNullOrEmpty(ProviderName))
     {
         Response.Redirect("~/Account/Login");
     }
     if (!IsPostBack)
     {
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new AcademyDbContext())).Authentication;
         var            auth            = Context.GetOwinContext().Authentication;
         ClaimsIdentity id     = manager.GetExternalIdentity(auth);
         IdentityResult result = manager.SignInExternalIdentity(auth, id);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else if (User.Identity.IsAuthenticated)
         {
             result = manager.LinkExternalIdentity(id, User.Identity.GetUserId());
             if (result.Success)
             {
                 OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
             }
             else
             {
                 AddErrors(result);
                 return;
             }
         }
         else
         {
             userName.Text = id.Name;
         }
     }
 }