Webmatrix入门网站 - 新用户需要确认电子邮件

时间:2015-01-28 17:26:31

标签: security razor webmatrix asp.net-webpages

我已根据初始网站模板附带的用户管理构建了我的网站。这非常有效,除了我需要为我的用例稍微修改它。让我解释一下:

目前,当新用户注册时,用户是在数据库中创建的,但不允许用户访问使用“WebSecurity.RequireAuthenticatedUser();”保护的任何页面。直到他们打开并点击确认电子邮件....

我想允许用户在确认他们的电子邮件之前访问我网站的某些部分,这可能吗?

我应该使用与WebSecurity.RequireAuthenticatedUser()不同的方法吗? 我应该在创建用户时删除“requireEmailConfirmation”开关??

目前,它看起来像这样:

                try {
                bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
                var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
                if (requireEmailConfirmation) {
                    var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
                    var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/owner/confirm?confirmationCode=" + HttpUtility.UrlEncode(token));

                    var details = "X-MC-MergeVars: {\"FIRSTNAME\": \"" + firstname + "\", \"LASTNAME\": \"" + lastname + "\", \"CONFIRMADDRESS\": \"" + confirmationUrl + "\"}";
                    var header = new[]{"X-MC-Template:registertemplate", "Reply-To:noreply@stayinflorida.co.uk", details};

                    WebMail.Send(
                        to: email,
                        subject: "Please confirm your account",
                        body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account.",
                        additionalHeaders: header
                    );
                }

                if (requireEmailConfirmation) {
                    // Thank the user for registering and let them know an email is on its way
                    Response.Redirect("~/owner/thanks");
                } else {
                    // Navigate back to the homepage and exit
                    WebSecurity.Login(email, password);

                    Response.Redirect("~/");
                }
            } catch (System.Web.Security.MembershipCreateUserException e) {
                ModelState.AddFormError(e.Message);
            }

1 个答案:

答案 0 :(得分:0)

对于必须注册使用的页面 这是在顶部

@{
    if (!WebSecurity.IsAuthenticated) {
        Response.Redirect("~/Account/Login?returnUrl="
            + "~/AllRaces.cshtml");
    }

    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Upcoming";
}

对于任何人都可以查看的页面 这是在顶部

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Contact";
}