コード例 #1
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase avatar)
        {
            if (ModelState.IsValid)
            {
                //var user = new ApplicationUser
                //{
                //    FirstName = model.FirstName,
                //    LastName = model.LastName,
                //    DisplayName = model.DisplayName,
                //    UserName = model.Email,
                //    Email = model.Email,
                //    //AvatarUrl = WebConfigurationManager.AppSettings["DefaultAvatar"]
                //};

                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName, AvatarUrl = "/Avatars/default_user.png"
                };

                if (avatar != null)
                {
                    if (ImageUploadValidator.IsWebFriendyIMage(avatar))
                    {
                        var fileName     = Path.GetFileName(avatar.FileName);
                        var justFileName = Path.GetFileNameWithoutExtension(fileName);
                        justFileName = StringUtilities.URLFriendly(justFileName);
                        fileName     = $"{justFileName}_{DateTime.Now.Ticks}{Path.GetExtension(fileName)}";

                        avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                        user.AvatarUrl = "/Avatars/" + fileName;
                    }
                }

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking " + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
コード例 #2
0
        public ActionResult Create([Bind(Include = "Id,Created,Updated,Title,Abstract,Slug,Body,MediaURL,Published")] BlogPost blogPost, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (ImageUploadValidator.IsWebFriendyIMage(image))
                {
                    if (image != null && image.ContentLength > 0)
                    {
                        var fileName    = Path.GetFileName(image.FileName);
                        var extension   = Path.GetExtension(image.FileName);
                        var newFileName = Path.GetFileNameWithoutExtension(image.FileName) + DateTime.Now.Ticks + extension;


                        var path = Path.Combine(Server.MapPath("~/Uploads"), newFileName);
                        image.SaveAs(path);
                        blogPost.MediaURL = newFileName;
                    }

                    var Slug = StringUtilities.URLFriendly(blogPost.Title);
                    if (String.IsNullOrWhiteSpace(Slug))
                    {
                        ModelState.AddModelError("Title", "Invalid title");
                        return(View(blogPost));
                    }
                    if (db.BlogPosts.Any(p => p.Slug == Slug))
                    {
                        ModelState.AddModelError("Title", "The title must be unique");
                        return(View(blogPost));
                    }
                    blogPost.Slug    = Slug;
                    blogPost.Created = DateTimeOffset.Now;


                    db.BlogPosts.Add(blogPost);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }//End of image validation if statement
                else
                {
                    ModelState.AddModelError("MediaURL", "The photo is too big or is of a wrong type.");
                }
            }

            return(View(blogPost));
        }