登录控件的登录事件未触发 [英] Login control's loggedin event not firing

查看:44
本文介绍了登录控件的登录事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到论坛上到处都是这个问题的例子,但唯一的解决方案是指没有实际连接的事件或错别字.我很确定我的问题与这些都无关!

I've noticed that forums are littered with examples of this problem, but with the only solutions referring to events not actually being hooked up, or typos. I'm pretty sure that my problem relates to neither of these!

我有一个带有开始标记的登录控件,如下所示:

I have a login control with an opening tag that looks like this:

<asp:Login ID="signInControl" FailureText="Sorry, your login has not been successful" runat="server" DisplayRememberMe="false" OnLoggedIn="signInControl_LoggedIn" OnAuthenticate="signInControl_Authenticate" OnLoggingIn="signInControl_LoggingIn">

这是我在代码隐藏中的事件:

Here are my events in the code-behind:

protected void signInControl_LoggedIn(object sender, EventArgs e)
        {
            MembershipProvider provider = Membership.Providers[GlobalConstants.MembershipProviderName];
            MembershipUser user = provider.GetUser(this.signInControl.UserName, true);

            int expiryInDays = Utility.GetConfigurationValueAsInt(SPContext.Current.Web, "PasswordExpiryLengthInDays");

            // provide a mechanism to stop password expiry (i.e. if -1);
            if (expiryInDays > 0)
            {
                expiryInDays = expiryInDays * -1;

                // If the user hasn't changed their password within the last x days, send them to update it.
                DateTime minimumPasswordChange = DateTime.Now.AddDays(expiryInDays);

                TimeSpan due = user.LastPasswordChangedDate.Subtract(minimumPasswordChange);

                Logging.LogTrace(string.Format("The user {0} next password change is in {1} days.", user.UserName, due.Days));

                if (user.LastPasswordChangedDate >= minimumPasswordChange)
                {
                    SPUtility.Redirect("/_layouts/ExpiredPassword.aspx", SPRedirectFlags.Trusted, this.Context);
                }
            }
            else
            {
                SPUtility.Redirect("/_layouts/ExpiredPassword.aspx", SPRedirectFlags.Trusted, this.Context);
            }
        }
protected void signInControl_Authenticate(object sender, AuthenticateEventArgs e)
        {
            SPClaimsUtility.AuthenticateFormsUser(new Uri(SPContext.Current.Web.Url), this.signInControl.UserName, this.signInControl.Password);
            e.Authenticated = Membership.ValidateUser(this.signInControl.UserName, this.signInControl.Password);
        }
protected void signInControl_LoggingIn(object sender, LoginCancelEventArgs e)
        {
            Logging.LogTrace(string.Format("User {0} attempting to sign in.", this.signInControl.UserName));
            e.Cancel = false;
        }

正如您在身份验证事件中看到的那样,e.authenticated 由用户是否可以通过验证来设置.我已经通过进入返回 true 的代码确认.那么为什么我的 signInControl_LoggedIn 方法没有触发?我真的很困惑!

And as you can see in the authenticate event, e.authenticated is being set by whether the user can be validated. I've confirmed by stepping into the code that true is returned. So why isn't my signInControl_LoggedIn method firing? I'm seriously confused!

这是前端的标记:

感谢您的帮助!

卡尔.

推荐答案

有许多帖子描述了如何为 SharePoint 2010 创建您自己的自定义登录页面.这里是我关注的.通常,他们创建一个继承自 FormsSignInPage 的应用程序页面.对于只需要更改外观的页面,这很有效.在某些情况下,例如这种情况,以这种方式创建登录页面可能还不够.

There are number of posts that describe how to create your own custom login page for SharePoint 2010. Here is the one I followed. In general, they create an application page that inherits from FormsSignInPage. For a page that just needs to change the look and feel, this works well. In some cases, like this one, creating the login page in this way may not be enough.

在本post,您需要继承自 IdentityModelSignInPageBase 而不是 FormsSignInPage.您还需要为身份验证事件创建一个事件处理程序,并将其注册到页面的 OnLoad 事件中.

The way it is described at the end of this post, you need to inherit from IdentityModelSignInPageBase instead of FormsSignInPage. You also need to create an event handler for the authenticate event and register this in the OnLoad event of the page.

这是因为控件中的 Authenticate 方法会在调用 OnLoggedIn 事件之前自动将用户带到ReturnUrl"页面.这种方法只需要两行代码就可以验证用户.

This is because the Authenticate method in the control will automatically take the user to the "ReturnUrl" page before it ever calls the OnLoggedIn event. It only takes two lines of code to authenticate the user in this method.

void signInControl_Authenticate(object sender, AuthenticateEventArgs e)
{
  Login signInControl = sender as Login;
  e.Authenticated = SPClaimsUtility.AuthenticateFormsUser(this.Page.Request.Url, signInControl.UserName, signInControl.Password);
}

由于您的代码现在不会将用户发送到 ReturnUrl 值,因此已到达 OnLoggedIn 事件.

Since your code now does not send the user to the ReturnUrl value, the OnLoggedIn event is reached.

如果我必须创建自定义 Authenticate 方法,您可能会问为什么我需要 OnLoggedIn 事件.好吧,控件仍然会为您处理一些功能.例如,如果用户未进行身份验证(即错误的 UN 和 PW 组合),控件将显示错误消息并且 OnLoggedIn 永远不会触发.

You may ask why do I need the OnLoggedIn event if I have to create a custom Authenticate method. Well, there are still functions the control will handle for you. For instance, if the user does not authenticate (ie bad UN and PW combination), the control will display the error message and the OnLoggedIn never fires.

这篇关于登录控件的登录事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆