简体   繁体   中英

How to check if user is logged in (authenticated) in Sitefinity widget template?

I need to display a piece of text via widget template to users that are not logged in. The widget template is serving a Sitefinity dynamic module, the module is partially available to non-logged in users. I want the non-logged in users to get a message that they should login in order to read the full content.

I have set the permissions for the fields and everything is working as it should but the only missing piece is displaying the message to non-logged in users.

I tested this in a simple web form test file and it works:

<p>You are <asp:Literal ID="UserStatus" runat="server" />.</p>

and the Code-behind is:

public string Name { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.Request.IsAuthenticated)
        {
            UserStatus.Text = "Logged in";
        }
        else
        {
            UserStatus.Text = "Not logged in";
        }
    }

I know there is a way to add Code-behind to widget templates in Sitefinity but when I try to include the above Code-behind to my widget template, I get this error:

Object reference not set to an instance of an object.

and the error is pointing to the line:

UserStatus.Text = "Logged in";

How to accomplish this via Code-behind and widget template or any other solutions or methods?

Because the Sitefinity widget templates are essentially just ascx files what you can do is create another custom user control and then reference that from your widget template instead of going the code behind route.

I've modified your code to use the Sitefinity api to check if the user is logged in, you can use this in the page load event for your new control that you'll register on the widget template:

protected void Page_Load(object sender, EventArgs e)
{

    if  (ClaimsManager.GetCurrentIdentity().IsAuthenticated)
        UserStatus.Text = "Logged in";
    else
        UserStatus.Text = "Not logged in";
}

You will need the following using directive:

using Telerik.Sitefinity.Security.Claims;

After you've created your custom control you can reference it from the widget template using either an Import or Register directive:

<%@ Register TagName="LoginStatus" TagPrefix="Custom" Src="~/Controls/LoginStatus/LoginStatus.ascx" %>

You can then use the control like this:

<Custom:LoginStatus runat="server" />

Here's another post sort of related to this: SiteFinity if user logged in

Do you have autoeventwireup="true"?

If that doesn't help you could make the text of the literal like this:

" />

Something like that above. Sorry doing this on a phone so syntax is abit off. Also look in the widget template editor for that widget it shoul have a property on the right side for auth that you can insert.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM