YAFLogo

Marvel
  • Marvel
  • 63.2% (Friendly)
  • YAF Forumling Topic Starter
15 years ago
We are not using asp providers on main site.

When user login on main site, automatic forum authentification is expected.

I'm thinking about synchronizing user databases.

So question is how to Login/ Logout on forum from code on parent site?

Sponsor
Marvel
  • Marvel
  • 63.2% (Friendly)
  • YAF Forumling Topic Starter
15 years ago
Found it by myself. Answer is:

FormsAuthentication.SetAuthCookie(actId.ToString(), persistentCookie);

someguy
  • someguy
  • 50.6% (Neutral)
  • YAF Forumling
15 years ago
Our site also has its own user database (no membership, just simple login/password stored in a table). In order to accomplish login and database user table synchronization, we added something like the following at the end of our application's login method. There are some constants in the example below (e.g. yafPassword, appName) that could be placed in a config file but are shown inline for simplicity.



                if (successfulLogin)
                {
                    // Successful login.
                    // Sign user in to Yet Another Forum (YAF).

                    // All accounts in the YAF table have the same hard-coded password.
                    // This allows us to avoid having to synch password changes with our own user tables.
                    string yafPassword = "-";

                    // The application name here must match applicationName setting in web.config membership section.
                    string appName = "YetAnotherForum"; 

                    object userKey = YAF.Providers.Profile.DB.GetProviderUserKey(appName, userName);

                    DataRow yafUser = YAF.Providers.Membership.DB.GetUser(appName, userKey, userName, true);
                    
                    if (yafUser == null)
                    {
                        // User not registered in YAF forum.
                        // Register the user now.
                        YAF.Providers.Membership.DB.CreateUser(appName, userName, yafPassword, "", 0, Email, "", "", true, YAF.Providers.Profile.DB.GetProviderUserKey("YetAnotherForum", userName));
                    }

                    // Set the YAF cookie
                    System.Web.Security.FormsAuthentication.SetAuthCookie(userName, true);

                }

In our logout method we call:


                FormsAuthentication.SignOut();

And our web.config is essentially the .NET 3.5 recommended config with the following section:



    <authentication mode="Forms">
      <forms name="login" loginUrl="/SignIn.aspx" timeout="30" path="/" />
    </authentication>

Note that if you go this route you will also want to disable user login/logout and user registration in your forum (you can do this by logging in to your forum as an administrator and then editing the host settings (Admin -> Host Settings; check Disable New Registrations, uncheck Allow Login and Logoff).

Finally, you will likely want to override YAF's profile pages. We accomplished this by editing the YAF source code to change any/all profile links so they point to our own profile pages. Something like this in YAF.Classes.Utils:



	/// <summary>
	/// Static class with link building functions.
	/// </summary>
	public static class YafBuildLink
	{
		/// <summary>
		/// Gets link to the page.
		/// </summary>
		/// <param name="page">Page to which to create a link.</param>
		/// <returns>URL to the given page.</returns>
		static public string GetLink(ForumPages page)
        {
            string link = "";

            switch (page)
            {
                case ForumPages.profile:
                    link = "/users/profile.aspx";
                    break;
                case ForumPages.cp_profile:
                    link = "/myprofile.aspx";
                    break;
                default:
                    link = Config.UrlBuilder.BuildUrl(string.Format("g={0}", page));
                    break;
            }

            return link;
		}
		/// <summary>
		/// Gets link to the page with given parameters.
		/// </summary>
		/// <param name="page">Page to which to create a link.</param>
		/// <param name="format">Format of parameters.</param>
		/// <param name="args">Array of page parameters.</param>
		/// <returns>URL to the given page with parameters.</returns>
		static public string GetLink(ForumPages page, string format, params object[] args)
		{
            string link = "";

            switch (page)
            {
                case ForumPages.profile:
                    link = String.Format("/users/profile.aspx?yafuserid={0}", args);
                    break;
                case ForumPages.cp_profile:
                    link = "/myprofile.aspx";
                    break;
                default:
                    link = Config.UrlBuilder.BuildUrl(string.Format("g={0}&{1}", page, string.Format(format, args)));
                    break;
            }

			return link;
		}