【问题标题】:Webmatrix starter site - new users require email confirmationWebmatrix 入门网站 - 新用户需要电子邮件确认
【发布时间】:2015-01-28 17:26:31
【问题描述】:

我已经基于初始站点模板附带的用户管理构建了我的站点。这很好用,除了我需要为我的用例稍微修改它。让我解释一下:

目前,当新用户注册时,会在数据库中创建用户,但不允许用户访问任何使用“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);
            }

【问题讨论】:

  • 在确认注册之前,您如何阻止这些用户访问您希望他们能够访问的页面?
  • 嗨迈克,目前我所有的“所有者”页面都使用 WebSecurity.RequireAuthenticatedUser();方法。但从测试来看,这仅在用户通过电子邮件确认其帐户后才有效。
  • 我想要实现的不是等待他们确认他们的电子邮件,而是在确认之前他们仍然可以访问某些页面。问题是,我需要使用 WebSecurity.CurrentUserId 在这些页面上执行一些 SQL 调用;方法,我只能在用户登录后才能执行,对吗?
  • 是的。如果您要求用户进行身份验证,他们必须拥有一个有效的帐户并登录。您可以通过让用户登录以识别他们来绕过它。但是,这些页面的登录表单不会使用 WebSecurity 帮助程序。您将直接查询数据库 - 只是为了验证他们尝试注册的事实。

标签: security razor webmatrix asp.net-webpages


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多