简体   繁体   English

如何检查用户是否在Sitefinity小部件模板中登录(已验证)?

[英]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. 我需要通过窗口小部件模板向未登录的用户显示一段文本。窗口小部件模板为Sitefinity动态模块提供服务,该模块可供未登录用户部分使用。 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: 我在一个简单的Web表单测试文件中测试了它,它可以工作:

<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: 我知道有一种方法可以在Sitefinity中为窗口小部件模板添加代码隐藏,但是当我尝试将上面的代码隐藏在我的窗口小部件模板中时,我收到此错误:

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. 因为Sitefinity窗口小部件模板本质上只是ascx文件,所以您可以创建另一个自定义用户控件,然后从窗口小部件模板引用它而不是路由后面的代码。

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: 我已经修改了你的代码以使用Sitefinity api来检查用户是否已登录,你可以在页面加载事件中使用它来为你将在widget模板上注册的新控件:

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指令:

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: 创建自定义控件后,可以使用Import或Register指令从窗口小部件模板中引用它:

<%@ 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 这是另一篇与此相关的帖子: 如果用户登录,则为SiteFinity

Do you have autoeventwireup="true"? 你有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. 另外,在窗口小部件模板编辑器中查看该窗口小部件,它应该在右侧有一个属性,您可以插入auth。

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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