Exemplo n.º 1
0
 protected void CreateUser_Click(object sender, EventArgs e)
 {
     var manager = this.Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
     var user = new User
                    {
                        UserName = this.Email.Text,
                        Email = this.Email.Text,
                        FirstName = this.FirstName.Text,
                        LastName = this.LastName.Text,
                        AvatarUrl = GlobalConstants.DefaultUserAvatar
                    };
     var result = manager.Create(user, this.Password.Text);
     if (result.Succeeded)
     {
         // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
         // string code = manager.GenerateEmailConfirmationToken(user.Id);
         // string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
         // manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
         IdentityHelper.SignIn(manager, user, false);
         if (!Roles.RoleExists("user"))
         {
             Roles.CreateRole("user");
         }
         Roles.AddUserToRole(user.UserName, "user");
         IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
     }
     else
     {
         Notifier.Error(result.Errors.FirstOrDefault());
     }
 }
Exemplo n.º 2
0
 public static void SignIn(ApplicationUserManager manager, User user, bool isPersistent)
 {
     var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
     authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
     authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
 }
Exemplo n.º 3
0
 private void RemoveOldAvatar(User user)
 {
     if (user.AvatarUrl != GlobalConstants.DefaultUserAvatar)
     {
         var oldAvatarPath = this.Server.MapPath(GlobalConstants.ImagesPath + user.AvatarUrl);
         File.Delete(oldAvatarPath);
     }
 }
        private void CreateAndLoginUser()
        {
            if (!this.IsValid)
            {
                return;
            }
            var manager = this.Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var user = new User { UserName = this.email.Text, Email = this.email.Text };
            var result = manager.Create(user);
            if (result.Succeeded)
            {
                var loginInfo = this.Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    this.RedirectOnFail();
                    return;
                }
                result = manager.AddLogin(user.Id, loginInfo.Login);
                if (result.Succeeded)
                {
                    IdentityHelper.SignIn(manager, user, false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // var code = manager.GenerateEmailConfirmationToken(user.Id);
                    // Send this link via email: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id)

                    IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
                    return;
                }
            }
            this.AddErrors(result);
        }