Example #1
0
 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).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;
         }
     }
 }
Example #2
0
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var user = new User(userName.Text);
            IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
            IdentityResult         result  = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user);

            if (result.Success)
            {
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                AddErrors(result);
                return;
            }
        }
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName     = UserName.Text;
            string fullName     = this.FullName.Text;
            string email        = this.Email.Text;
            string summary      = this.Summary.Text;
            string localPath    = string.Empty;
            string relativePath = string.Empty;

            if (FileUploadControl.HasFile)
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);
                localPath    = Server.MapPath("~/Uploaded_Files/") + filename;
                relativePath = "/Uploaded_Files/" + filename;
                FileUploadControl.SaveAs(localPath);
                StatusLabel.Text = "Upload status: File uploaded!";
            }

            var             manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            var             context = new ApplicationDbContext();
            ApplicationUser u       = new ApplicationUser()
            {
                UserName = userName, FullName = fullName, Email = email, Summary = summary, AvatarUrl = relativePath,
            };

            var adminId = context.Roles.FirstOrDefault(r => r.Name == "User").Id.ToString();

            manager.Roles.AddUserToRoleAsync(u.Id, adminId);

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

            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
Example #4
0
 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 ApplicationDbContext())).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;
         }
     }
 }