Pergunta

i installed yet another forum(yaf) code successfully and integrated it with blog engine 2.0 successfully.Now I wanted to have the common login for both blog engine 2.0 and yaf i.e when i login for blogengine2.0 automatically yaf has to be logged in .Can anyone suggest me the solution?

Foi útil?

Solução

Change the MembershipPorivder in the webconfig to the following:

//I have changaned the following to be able to genrate a password myself and to be    able to retrive it when i want to login the use, you can change this if you want or just leave it as it is.
<membership defaultProvider="YafMembershipProvider" hashAlgorithmType="NONE" >
        <providers>
            <clear/>
            <add connectionStringName="yafnet" applicationName="YetAnotherForum" name="YafMembershipProvider" passwordFormat="Clear" requiresUniqueEmail="true" useSalt="false" enablePasswordRetrieval="true" type="YAF.Providers.Membership.YafMembershipProvider"/>
        </providers>
    </membership>

This method here will create a YAFUser, all these methods are extracted from code the YAF library i just worked a lot on them to extract everything i want:

    /// <summary>
    /// Register YAF User
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="email"></param>
    /// <param name="displayName"></param>
    public static MembershipUser RegisterYAF_User(string userName, string password, string email, string displayName)
    {
        //Intilize YAFMembershipProvider
        MembershipCreateStatus status;
        YafMembershipProvider provider = new YafMembershipProvider();
        NameValueCollection valueCollection = new NameValueCollection();
        valueCollection.Add("connectionStringName", "yafnet");
        valueCollection.Add("applicationName", "YetAnotherForum");
        valueCollection.Add("passwordFormat", "Clear");
        provider.Initialize("YafMembershipProvider", valueCollection);

        //Register YAFMembershipUser
        MembershipUser user = provider.CreateUser(userName, password, email, "What is your favorite football team ?", "None", true, new YAF.Providers.Profile.DB().GetProviderUserKey("YetAnotherForum", userName), out status);

        //Register YAFUser
        if (user != null)
        {
            //Add user to role
            RoleMembershipHelper.AddUserToRole(user.UserName, "Registered");

            // setup inital roles (if any) for this user
            RoleMembershipHelper.SetupUserRoles(YafContext.Current.PageBoardID, userName);

            // create the user in the YAF DB as well as sync roles...
            int? userID = RoleMembershipHelper.CreateForumUser(user, user.UserName, YafContext.Current.PageBoardID);

            // create empty profile just so they have one
            YafUserProfile userProfile = YafUserProfile.GetProfile(user.UserName);

            // setup their inital profile information
            userProfile.Location = "USA";
            userProfile.Homepage = string.Empty;
            userProfile.Save();

                 LegacyDb.user_save(
            UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey),
            YafContext.Current.PageBoardID,
            null,
            null,
            null,
            YafContext.Current.TimeZoneUser.ToType<int>(),
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null);
             }

        return user;
    }

This function is used to Login the YAFUser:

        //Login yaf user
    /// <summary>
    /// Login
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public static void LoginYAF_User(string userName, string password)
    {
        MembershipCreateStatus status;
        YafMembershipProvider provider = new YafMembershipProvider();
        NameValueCollection valueCollection = new NameValueCollection();
        valueCollection.Add("connectionStringName", "yafnet");
        valueCollection.Add("applicationName", "YetAnotherForum");
        valueCollection.Add("passwordFormat", "Clear");
        provider.Initialize("YafMembershipProvider", valueCollection);

        bool Authenticated = false;
        if (provider.ValidateUser(userName, password))
        {
            Authenticated = true;
            FormsAuthentication.SetAuthCookie(userName, true);
        }
        else if (new YafBoardSettings().EnableDisplayName)
        {
            var id = new DefaultUserDisplayName(YafContext.Current.ServiceLocator).GetId(userName);

            if (id.HasValue)
            {
                // get the username associated with this id...
                userName = UserMembershipHelper.GetUserNameFromID(id.Value);

                // validate again...
                if (provider.ValidateUser(userName, password))
                {
                    Authenticated = true;
                }
            }
        }

        //These 2 lines of code is to redirect to the fourm main page
        //YafContext.Current.Get<IRaiseEvent>().Raise(new SuccessfulUserLoginEvent(YafContext.Current.CurrentUserData.UserID));
        //YafBuildLink.Redirect(ForumPages.forum);
    }

After that go to YAFMembershipProvider.cs and add an overload method like the following:

    public string GetPassword(string username)
{
    // Check for null arguments
    if ((username == null))
    {
        ExceptionReporter.ThrowArgument("MEMBERSHIP", "USERNAMEPASSWORDNULL");
    }

    UserPasswordInfo currentPasswordInfo = UserPasswordInfo.CreateInstanceFromDB(
      "YetAnotherForum",
      username,
      false,
      this.UseSalt,
      this.HashHex,
      this.HashCase,
      "",
      this.MSCompliant);

    return currentPasswordInfo.Password;
}

In this i have given a 99% percent, the rest is just plain simple coding, as for the blog you should use the same concept. Search through the DataLayer and get all the methods that you need to sync between as many applications as you want.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top