locked
needs approach for authenticated users to access some resources RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I have developed a website below is the header of website.

    The website has some content which is accessible for for everyone.

    This page also has a gridview with some data and a button field named  "Book".  I want only logged in users can access this button "Book"

    What approach I should use? In header I am using <a href  >link for Sine In.

    How can i change same <a href link> "Sine In" to "Log Out" after login?

    I need only approach.

    Monday, January 21, 2019 11:14 AM

Answers

  • User-893317190 posted

    Hi demoninside9,

    It seems you want to control the ui of your page according to the user state.

    If so , for the a link , you could try the syntax below.

    <% presents the start of c# code , %> represents the end of c# code, you could insert  asp markup  between %> and <%,

    then if user has logged in , logout link will show, else sign in link will show.

        <%
    if ( logic to determine whether the user has logged in )
    {
    %>
    <asp:LinkButton ID="LinkButton1" runat="server">logout</asp:LinkButton>
    <%}
    else
    {
    %>
    <asp:LinkButton ID="LinkButton2" runat="server">sign in</asp:LinkButton>
    <%
    }
    %>

    But this syntax could not be used in gridview.

    So I suggest you could use rowdatabound event to change the content of your gridview according to whether the user has logged in.

    For example , you could write your aspx as follows .

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
    <%-- omit other rows --%> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" Text="Button" ID="book" /> <asp:Label ID="Label4" runat="server" Text="Please log in"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    Then in code behind you could 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (write your code to determine whether the user has logged in)
                    {
                        e.Row.FindControl("book").Visible = true;
                        e.Row.FindControl("Label4").Visible = false;
                    }
                    else
                    {
                        e.Row.FindControl("book").Visible = false;
                        e.Row.FindControl("Label4").Visible = true;
                    }
                   
                }
            }

    Or you could directly remove the row is user hasn't logged in. using GridView.Columns.Remove

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2019 4:02 AM

All replies

  • User753101303 posted

    Hi,

    Seems you are looking for System.Web.HttpContext.User.Identity.IsAuthenticated (and/.or IsInRole) to find out if this link should be shown. User Is also exposed as property for the Page control.

    Edit: for web forms you also have the https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.loginview?view=netframework-4.7.2 control

    Monday, January 21, 2019 12:26 PM
  • User-893317190 posted

    Hi demoninside9,

    It seems you want to control the ui of your page according to the user state.

    If so , for the a link , you could try the syntax below.

    <% presents the start of c# code , %> represents the end of c# code, you could insert  asp markup  between %> and <%,

    then if user has logged in , logout link will show, else sign in link will show.

        <%
    if ( logic to determine whether the user has logged in )
    {
    %>
    <asp:LinkButton ID="LinkButton1" runat="server">logout</asp:LinkButton>
    <%}
    else
    {
    %>
    <asp:LinkButton ID="LinkButton2" runat="server">sign in</asp:LinkButton>
    <%
    }
    %>

    But this syntax could not be used in gridview.

    So I suggest you could use rowdatabound event to change the content of your gridview according to whether the user has logged in.

    For example , you could write your aspx as follows .

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
    <%-- omit other rows --%> <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" Text="Button" ID="book" /> <asp:Label ID="Label4" runat="server" Text="Please log in"></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    Then in code behind you could 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if(e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (write your code to determine whether the user has logged in)
                    {
                        e.Row.FindControl("book").Visible = true;
                        e.Row.FindControl("Label4").Visible = false;
                    }
                    else
                    {
                        e.Row.FindControl("book").Visible = false;
                        e.Row.FindControl("Label4").Visible = true;
                    }
                   
                }
            }

    Or you could directly remove the row is user hasn't logged in. using GridView.Columns.Remove

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2019 4:02 AM
  • User1052662409 posted

     <asp:LinkButton ID="LinkButton2" runat="server">sign in</asp:LinkButton>

    It is not a Linkbutton, it's a href link <a class="page-scroll" data-toggle="modal" data-target="#myModal" href="#">Sine In</a>

    Tuesday, January 22, 2019 4:34 AM
  • User-893317190 posted

    Hi demoninside9,

    It could also applies to a href link.

    Tuesday, January 22, 2019 5:05 AM