locked
Login_authenticate event is not getting called RRS feed

  • Question

  • Hi,

    I developed a webpart and placed loginview in usercontrol.

    Login_authenticate event is not getting called when i put the webpart in masterpage..

    if i put it on content page its working fine.

    loggingout event is getting called from the same webpart.

    Did anyone face a similar issue?

    Tuesday, November 12, 2013 8:19 AM

Answers

  •  I was missing the event call onauthenticate in the .ascx markup<asp:Login runat="server" ID="Login1" Font-Size="8pt" PasswordLabelText="Password"
                LoginButtonText="SignIn" UserNameLabelText="User Name" Font-Names="Tahoma, &quot;Microsoft Sans Serif&quot;"
                BorderPadding="3" CreateUserText="» New User" CreateUserUrl="/English/Pages/Register.aspx"
                PasswordRecoveryText="» Forget my password" PasswordRecoveryUrl="/English/Pages/ForgetPassword.aspx"
                CssClass="TxtCenter" TextLayout="TextOnTop" DisplayRememberMe="False" Width="193px"
                MembershipProvider="MembershipProvider" VisibleWhenLoggedIn="False"
                DestinationPageUrl="/" onauthenticate="Login1_Authenticate">
                <CheckBoxStyle CssClass="chkRember" />
                <TextBoxStyle Width="100px" CssClass="TxtBox" />
                <LoginButtonStyle CssClass="SubmitBtn" />
                <TitleTextStyle CssClass="Hide" />
                <HyperLinkStyle CssClass="LinkSml-Green" />
            </asp:Login>

    It is working now. It was simple.

    Cheers.

    • Marked as answer by MAK185 Sunday, November 17, 2013 8:37 AM
    Sunday, November 17, 2013 8:36 AM

All replies

  • hi

    how you put the web part on the masterpage? Can you show the code?


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Wednesday, November 13, 2013 1:20 PM
  • Hi sadomovalex,

    I added the webpart from designer.

    I think the problem is related with the permissions.

    For anonymous users the events are not getting called.

    For authenticated users it works well.

    I have give anonymous access to the Master Page Gallery and Style library.

    The flash file on the home page doesn't load for anonymous user.

    Please help me out guys. Need to fix this asap.

    Thursday, November 14, 2013 5:48 AM
  • LoginView control has different templates for anonymous and authenticated users (AnonymousTemplate andLoggedInTemplate). Did you define both? Again, put your code here both for layout and codebehind, it will be easier to check.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Thursday, November 14, 2013 12:20 PM
  • <asp:LoginView runat="server" ID="MyLoginView">
        <AnonymousTemplate>
            <asp:Login runat="server" ID="Login1" Font-Size="8pt" PasswordLabelText="Password"
                LoginButtonText="SignIn" UserNameLabelText="User Name" Font-Names="Tahoma, &quot;Microsoft Sans Serif&quot;"
                BorderPadding="3" CreateUserText="» New User" CreateUserUrl="/English/Pages/Register.aspx"
                PasswordRecoveryText="» Forget my password" PasswordRecoveryUrl="/English/Pages/ForgetPassword.aspx"
                CssClass="TxtCenter" TextLayout="TextOnTop" DisplayRememberMe="False" Width="193px"
                MembershipProvider="#######" VisibleWhenLoggedIn="False" DestinationPageUrl="/">
                <CheckBoxStyle CssClass="chkRember" />
                <TextBoxStyle Width="100px" CssClass="TxtBox" />
                <LoginButtonStyle CssClass="SubmitBtn" />
                <TitleTextStyle CssClass="Hide" />
                <HyperLinkStyle CssClass="LinkSml-Green" />
            </asp:Login>
        </AnonymousTemplate>
        <LoggedInTemplate>
            <table border="0" cellpadding="0" cellspacing="0" class="LogedInTbl">
                <tr>
                    <td>
                        <asp:Label CssClass="LoginTxt" ID="lblwelcome" runat="server" Text="Welcome">
                        </asp:Label>
                    </td>
                    <td>
                        <asp:LoginName CssClass="LoginTxt" runat="server" ID="LoginName1"></asp:LoginName>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:LoginStatus runat="server" LogoutText="LogOut" CssClass="LogoutBtn" ID="LoginStatus1"
                            LogoutPageUrl="/english" LogoutAction="Redirect"></asp:LoginStatus>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:HyperLink ID="EditProfile" runat="server" Text="EditProfile" CssClass="LinkSml-Green"
                            Target="_self" NavigateUrl="/English/Pages/EditProfile.aspx"></asp:HyperLink>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:HyperLink ID="ChangePassword" runat="server" Text="Change my password" CssClass="LinkSml-Green"
                            Target="_self" NavigateUrl="/English/Pages/ChangePassword.aspx"></asp:HyperLink>
                    </td>
                </tr>
            </table>
        </LoggedInTemplate>
    </asp:LoginView>




    and the codebehind is :

    protected void Page_Load(object sender, EventArgs e)
            {
                if (Request.IsAuthenticated)
                {
                    LoginName loginName = (LoginName)MyLoginView.FindControl("LoginName1");

                    if (loginName != null)
                    {
                        string userName = Page.User.Identity.Name;
                        if (userName.Contains("|") == true)
                        {
                            int length = userName.Length;
                            int index = userName.LastIndexOf("|");
                            userName = userName.Substring(index + 1, (length - index) - 1);
                            loginName.FormatString = userName;

                        }
                    }
                }
                //else
                //{
                //     Login login = new Login();
                //     login.Authenticate += new AuthenticateEventHandler(Login1_Authenticate);  
                //}
            }

            protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    string membership = "MembershipProvider";
                    string role = "RoleProvider";
                    Login Login1 = (Login)MyLoginView.FindControl("Login1");
                    e.Authenticated = SPClaimsUtility.AuthenticateFormsUser(new Uri(SPContext.Current.Web.Url), Login1.UserName, Login1.Password);

                    if (!e.Authenticated) return;

                    SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(SPContext.Current.Web.Url), membership, role, Login1.UserName, Login1.Password);
                    if (token == null)
                    {

                        e.Authenticated = false;
                        return;
                    }
                    else
                    {

                        SPFederationAuthenticationModule module = SPFederationAuthenticationModule.Current;
                        module.SetPrincipalAndWriteSessionToken(token);
                        e.Authenticated = true;

                        SPUtility.Redirect(SPContext.Current.Web.Url, SPRedirectFlags.Trusted, this.Context);

                    }
                });
            }

            protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
            {
                FormsAuthentication.SignOut();
                var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
                // Clear .ASPAUTH cookie key.
                if (authCookie != null)
                {
                    var myCookie = new HttpCookie(authCookie.Name) { Expires = DateTime.Now.AddDays(-1) };
                    Response.Cookies.Add(myCookie);
                }

                SPIisSettings iisSettingsWithFallback = Microsoft.SharePoint.SPContext.Current.Site.WebApplication.GetIisSettingsWithFallback(Microsoft.SharePoint.SPContext.Current.Site.Zone);
                if (iisSettingsWithFallback.UseClaimsAuthentication)
                {
                    Microsoft.IdentityModel.Web.FederatedAuthentication.SessionAuthenticationModule.SignOut();
                    // Clear FedAuth Cookie key
                    Microsoft.IdentityModel.Web.FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie();
                }
                SPUtility.Redirect("/English", SPRedirectFlags.Default, Context);
            }

    The code works fine when i place the webpart in default page or any other page.

    But from the masterpage it throws an error on login as the Login_Authenticate event is not getting called.

    If any user is logged in then logout event fires.

    The issue is related to the permissions for anonymous user. I already gave anonymous user access wherever necessary.

    Thursday, November 14, 2013 2:47 PM
  • this code doesn't show how you subscribe on Authenticate and LoggingOut events: handlers are not specified declaratively and not added in the codebehind. The only code which shows attempt to subscribe is this:

    //else
    //{
    //     Login login = new Login();
    //     login.Authenticate += new AuthenticateEventHandler(Login1_Authenticate);  
    //}
    but it is commented. Since you said that event is fired for authenticated users, you subscribe on it somehow. I don't know exactly it is done currently, but if you did it like shown in commented code it is not correct. You created instance of Login control and subscribe to its event, while instead you should find the instance in Controls collection of current control (using recursive method with FindControl). Or better subscribe on events declaratively, you will have less problems in this case.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Friday, November 15, 2013 9:19 AM
  •  I was missing the event call onauthenticate in the .ascx markup<asp:Login runat="server" ID="Login1" Font-Size="8pt" PasswordLabelText="Password"
                LoginButtonText="SignIn" UserNameLabelText="User Name" Font-Names="Tahoma, &quot;Microsoft Sans Serif&quot;"
                BorderPadding="3" CreateUserText="» New User" CreateUserUrl="/English/Pages/Register.aspx"
                PasswordRecoveryText="» Forget my password" PasswordRecoveryUrl="/English/Pages/ForgetPassword.aspx"
                CssClass="TxtCenter" TextLayout="TextOnTop" DisplayRememberMe="False" Width="193px"
                MembershipProvider="MembershipProvider" VisibleWhenLoggedIn="False"
                DestinationPageUrl="/" onauthenticate="Login1_Authenticate">
                <CheckBoxStyle CssClass="chkRember" />
                <TextBoxStyle Width="100px" CssClass="TxtBox" />
                <LoginButtonStyle CssClass="SubmitBtn" />
                <TitleTextStyle CssClass="Hide" />
                <HyperLinkStyle CssClass="LinkSml-Green" />
            </asp:Login>

    It is working now. It was simple.

    Cheers.

    • Marked as answer by MAK185 Sunday, November 17, 2013 8:37 AM
    Sunday, November 17, 2013 8:36 AM