This project is read-only.

No Comments unless logged in

Topics: ASP.NET 2.0
Jun 12, 2009 at 9:54 PM

Was looking for a simple way to restict comments on the blog unless the person is logged in.  Can't seem to find anything about this in the forum.

I would like ot have a blog that is viewable but not allow comments unless they have logged in.

Any help?

Jun 12, 2009 at 10:39 PM

On the CommentView control (BlogEngine.Web\User Controls\CommentView.ascx) you just need to wrap the div for the "comment form" in an if statement that checks if the user is authenticated.  Doing this will allow everyone to read comments, but only authenticated users will me able to leave comments.  Below is the source:


    if (Page.User.Identity.IsAuthenticated) 
    <div id="comment-form">
        <img src="<%=Utils.RelativeWebRoot %>pics/ajax-loader.gif" alt="Saving the comment"
            style="display: none" id="ajaxLoader" />
        <span id="status"></span>
        <div class="commentForm">
            <p id="addcomment">
                <%=Resources.labels.addComment %></p>
            <% if (NestingSupported)
               { %>
            <asp:HiddenField runat="Server" ID="hiddenReplyTo" />
            <p id="cancelReply" style="display: none;">
                <a href="javascript:void(0);" onclick="BlogEngine.cancelReply();">
                    <%=Resources.labels.cancelReply %></a></p>
            <%} %>
            <label for="<%=txtName.ClientID %>">
                < %>*</label>
            <asp:TextBox runat="Server" ID="txtName" TabIndex="2" ValidationGroup="AddComment" />
            <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtName"
                ErrorMessage=" <%$Resources:labels, chooseOtherName %>" Display="dynamic" ClientValidationFunction="BlogEngine.checkAuthorName"
                EnableClientScript="true" ValidationGroup="AddComment" />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
                ErrorMessage="<%$Resources:labels, required %>" Display="dynamic" ValidationGroup="AddComment" /><br />
            <label for="<%=txtEmail.ClientID %>">
                < %>*</label>
            <asp:TextBox runat="Server" ID="txtEmail" TabIndex="3" ValidationGroup="AddComment" />
            <span id="gravatarmsg">
                <%if (BlogSettings.Instance.Avatar != "none" && BlogSettings.Instance.Avatar != "monster")
                  { %>
                (<%=string.Format(Resources.labels.willShowGravatar, "<a href=\"\" target=\"_blank\">Gravatar</a>")%>)
                <%} %>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail"
                ErrorMessage="<%$Resources:labels, required %>" Display="dynamic" ValidationGroup="AddComment" />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
                ErrorMessage="<%$Resources:labels, enterValidEmail%>" Display="dynamic" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                ValidationGroup="AddComment" /><br />
            <label for="<%=txtWebsite.ClientID %>">
                < %></label>
            <asp:TextBox runat="Server" ID="txtWebsite" TabIndex="4" ValidationGroup="AddComment" />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="Server" ControlToValidate="txtWebsite"
                ValidationExpression="(http://|https://|)([\w-]+\.)+[\w-]+(/[\w- ./?%&=;~]*)?"
                ErrorMessage="<%$Resources:labels, enterValidUrl %>" Display="Dynamic" ValidationGroup="AddComment" /><br />
            <% if (BlogSettings.Instance.EnableCountryInComments)
               { %>
            <label for="<%=ddlCountry.ClientID %>">
                < %></label>
            <asp:DropDownList runat="server" ID="ddlCountry" onchange="BlogEngine.setFlag(this.value)"
                TabIndex="5" EnableViewState="false" ValidationGroup="AddComment" />
            <asp:Image runat="server" ID="imgFlag" AlternateText="Country flag" Width="16" Height="11"
                EnableViewState="false" /><br />
            <br />
            <%} %>
            <span class="bbcode<%= !BlogSettings.Instance.ShowLivePreview ? " bbcodeNoLivePreview" : "" %>"
                title="BBCode tags">
                <%=BBCodes() %></span>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtContent"
                ErrorMessage="<%$Resources:labels, required %>" Display="dynamic" ValidationGroup="AddComment" /><br />
            <% if (BlogSettings.Instance.ShowLivePreview)
               { %>
            <ul id="commentMenu">
                <li id="compose" class="selected" onclick="BlogEngine.composeComment()">
                <li id="preview" onclick="BlogEngine.showCommentPreview()">
            <% } %>
            <div id="commentCompose">
                <label for="<%=txtContent.ClientID %>" style="display: none">
                <asp:TextBox runat="server" ID="txtContent" TextMode="multiLine" Columns="50" Rows="10"
                    TabIndex="6" ValidationGroup="AddComment" />
            <div id="commentPreview">
                <img src="<%=Utils.RelativeWebRoot %>pics/ajax-loader.gif" alt="Loading" />
            <br />
            <input type="checkbox" id="cbNotify" style="width: auto" tabindex="7" />
            <label for="cbNotify" style="width: auto; float: none; display: inline">
                <%=Resources.labels.notifyOnNewComments %></label><br />
            <br />
            <input type="button" id="btnSaveAjax" value="<%=Resources.labels.saveComment %>"
                onclick="if(Page_ClientValidate('AddComment')){BlogEngine.addComment()}" tabindex="7" />
            <asp:HiddenField runat="server" ID="hfCaptcha" />
<%} %>

Jun 12, 2009 at 11:59 PM

I tried the above, uploaded to the server and then added a new post.  When I clicked on save/publish I get a compilation error:

The name 'NestingSupported' does not exist in the current context


Not sure what to do next.

Jun 13, 2009 at 3:25 AM

"NestingSupported" is a property in BE 1.5 (and some earlier builds as well).  I'm guessing you're probably using BE 1.4.5 or earlier?

Instead of copying that exact code puredotnet posted, just wrap the existing <div id="comment-form"> with the IF statement.  So it'll look like what's below (bolded code is new).

    if (Page.User.Identity.IsAuthenticated)

<div id="comment-form">
    .... existing stuff ....

<%} %>