0

我成功安装了另一个论坛(yaf)代码,并将其与博客引擎 2.0 成功集成。现在我想为博客引擎 2.0 和 yaf 进行通用登录,即当我自动登录 blogengine2.0 时,必须登录 yaf。谁能建议我解决方案?

4

1 回答 1

1

将 webconfig 中的 MembershipPorivder 更改为以下内容:

//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>

这里的这个方法将创建一个 YAFUser,所有这些方法都是从 YAF 库的代码中提取的,我刚刚对它们做了很多工作以提取我想要的一切:

    /// <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;
    }

此函数用于登录 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);
    }

之后转到 YAFMembershipProvider.cs 并添加一个重载方法,如下所示:

    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;
}

在这我给出了 99% 的百分比,其余的只是简单的编码,至于博客你应该使用相同的概念。搜索 DataLayer 并获取您需要在任意数量的应用程序之间同步的所有方法。

于 2012-05-22T08:43:23.920 回答