コード例 #1
0
        public async Task <CreatedUserViewModel> CreateONGUser(string email)
        {
            var user         = new ApplicationUser(email);
            var tempPassword = GenerateRandomPassword.Generate(8);
            var result       = await _userManager.CreateAsync(user, tempPassword);

            if (!result.Succeeded)
            {
                throw new Exception();
            }

            await _userManager.AddToRoleAsync(user, Roles.ONG);

            return(new CreatedUserViewModel
            {
                User = user,
                TempPassword = tempPassword
            });
        }
コード例 #2
0
        public async Task <ActionResult> Create(CreateUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files[0];
                if (file.ContentLength > 0)
                {
                    var filename = Guid.NewGuid() + "-" + Path.GetFileName(file.FileName);

                    if (System.Configuration.ConfigurationManager.AppSettings["StorageAccountName"] != null)
                    {
                        var azureUploadLocation = await StorageHelper.UploadFileToStorage(file.InputStream, filename);

                        if (azureUploadLocation != null)
                        {
                            model.AppUser.PhotoUrl = azureUploadLocation;
                        }
                    }
                    else
                    {
                        var path = Path.Combine(Server.MapPath("~/UploadedImages"), filename);
                        file.SaveAs(path);

                        model.AppUser.PhotoUrl = "/UploadedImages/" + filename;
                    }
                }

                model.AppUser.UserName    = model.Email;
                model.AppUser.Email       = model.Email;
                model.AppUser.CreatedById = User.Identity.GetUserId();
                model.AppUser.CreatedOn   = DateTime.Now;

                var password = GenerateRandomPassword.Generate(12);

                var result = await base.UserManager.CreateAsync(model.AppUser, password);

                if (result.Succeeded)
                {
                    UserManager.AddToRole(model.AppUser.Id, RoleName);

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(model.AppUser.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = model.AppUser.Id, code = code }, protocol: Request.Url.Scheme);
                    var message     = String.Format(@"<html>

<head>
    <meta charset='UTF-8'>
    <meta content='width=device-width, initial-scale=1' name='viewport'>
    <meta content='telephone=no' name='format-detection'>
    <title></title>
</head>

<body>
    <div class='es-wrapper-color'>
        <table class='es-wrapper' width='100%' cellspacing='0' cellpadding='0'>
            <tbody>
                <tr>
                    <td class='esd-email-paddings' valign='top'>
                        <table class='es-header' cellspacing='0' cellpadding='0' align='center'>
                            <tbody>
                                <tr>
                                    <td class='es-adaptive esd-stripe' align='center'>
                                        <table class='es-header-body' width='600' cellspacing='0' cellpadding='0' align='center'>
                                            <tbody>
                                                <tr>
                                                    <td class='esd-structure es-p20t es-p20b es-p40r es-p40l' esd-general-paddings-checked='true' align='left'>
                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                            <tbody>
                                                                <tr>
                                                                    <td class='esd-container-frame' width='520' valign='top' align='center'>
                                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td class='esd-block-image es-m-p0l' align='center'>
                                                                                        <img src='https://ojp8zqasz32qat8n13om56p4-wpengine.netdna-ssl.com/wp-content/uploads/2015/11/TheAmazingRaceLogo.png' width='250'>
                                                                                    </td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <table class='es-content' cellspacing='0' cellpadding='0' align='center'>
                            <tbody>
                                <tr>
                                    <td class='esd-stripe' esd-custom-block-id='3109' align='center'>
                                        <table class='es-content-body' style='background-color: rgb(255, 255, 255);' width='600' cellspacing='0' cellpadding='0' bgcolor='#ffffff' align='center'>
                                            <tbody>
                                                <tr>
                                                    <td class='esd-structure es-p20t es-p20b es-p40r es-p40l' esd-general-paddings-checked='true' align='left'>
                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                            <tbody>
                                                                <tr>
                                                                    <td class='esd-container-frame' width='520' valign='top' align='center'>
                                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td class='esd-block-text' align='left'>
                                                                                        <h1 style='color: rgb(74, 126, 176);'>Welcome to The Race</h1>
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td class='esd-block-spacer es-p5t es-p20b' align='left'>
                                                                                        <table width='5%' height='100%' cellspacing='0' cellpadding='0' border='0'>
                                                                                            <tbody>
                                                                                                <tr>
                                                                                                    <td style='border-bottom: 2px solid rgb(153, 153, 153); background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%; height: 1px; width: 100%; margin: 0px;'></td>
                                                                                                </tr>
                                                                                            </tbody>
                                                                                        </table>
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td class='esd-block-text es-p10b' align='left'>
                                                                                        <br />
                                                                                        <p><span style='font-size: 16px; line-height: 150%;'>Hi {0},</span></p>
                                                                                        <br />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td class='esd-block-text' align='left'>
                                                                                        <p>You have been successfully registered to The Amazing Race. You can find your credential below:</p>
                                                                                        <br />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td class='esd-block-text' align='left'>
                                                                                        <p>
                                                                                            <b>Role:</b> {1}<br />
                                                                                            <b>Email/Username:</b> {2}<br />
                                                                                            <b>Password:</b> {3}<br />
                                                                                    </td>
                                                                                </tr>
                                                                                <tr>
                                                                                    <td class='esd-block-text' align='left'>
                                                                                        <br />
                                                                                        <p>
                                                                                            Best Regards,<br />
                                                                                            The Amazing Race Team
                                                                                        </p>
                                                                                    </td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td class='esd-structure es-p20t es-p20b es-p40r es-p40l' esd-general-paddings-checked='true' align='left'>
                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                            <tbody>
                                                                <tr>
                                                                    <td class='esd-container-frame' width='520' valign='top' align='center'>
                                                                        <table width='100%' cellspacing='0' cellpadding='0'>
                                                                            <tbody>
                                                                                <tr>
                                                                                    <td class='esd-block-spacer es-p20t es-p20b es-p5r' align='center'>
                                                                                        <table width='100%' height='100%' cellspacing='0' cellpadding='0' border='0'>
                                                                                            <tbody>
                                                                                                <tr>
                                                                                                    <td style='border-bottom: 1px solid rgb(255, 255, 255); background: rgba(0, 0, 0, 0) none repeat scroll 0% 0%; height: 1px; width: 100%; margin: 0px;'></td>
                                                                                                </tr>
                                                                                            </tbody>
                                                                                        </table>
                                                                                    </td>
                                                                                </tr>
                                                                            </tbody>
                                                                        </table>
                                                                    </td>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>", model.AppUser.Name, RoleName, model.Email, password);

                    await UserManager.SendEmailAsync(model.AppUser.Id, "Welcome to The Amazing Race Singapore", message);

                    TempData["MessageAlert"] = new Alert {
                        CssClass = "alert-success", Title = "Success!", Message = RoleName + " is successfully created."
                    };
                    return(RedirectToAction("Create"));
                }
                AddErrors(result);
            }

            ViewData["GenderOptions"] = GenderOptions;
            return(View(model));
        }