I cannot thank you enough for posting this. I have struggled with this for the past week. I understood perfectly well how MVC membership and profiles work. The part that I was missing was simply that I don't need to utilize the Forms Auth for YAF. This was driving me insane. Genius to try the cookie name in existing Startup.Auth for MVC. I was a bout to manually maintain a sych between the two since I wasted so much time on this.
I am going to add a little more code here for anyone else coming to this post.
If you have the the default account controller from the MVC Template from VS.NET (AccountController.cs), here are the changes you need to make to it.
1. Add the references.
using YAF.Utils;
using YAF.Core;
using System.Web.Security;
using YAF.Providers.Membership;
using System.Data;
2. Alter Register
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");
//mjh -- Persist the user in the YAF forums as well.
CreateYAFUser(user.UserName, model.Password, model.Email);
return RedirectToAction("Index", "Secure");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
3: ExternalLoginConfirmation
// POST: /Account/ExternalLoginConfirmation
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Manage");
}
if (ModelState.IsValid)
{
// Get the information about the user from the external login provider
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
CreateYAFUser(user.UserName, user.PasswordHash, model.Email);
return RedirectToLocal(returnUrl);
}
}
AddErrors(result);
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
4. Add CreateYAFUser (I added mine to AccountController as static. Put it where ever you want.)
#region YAF User Sync with MVC Membership
private static void CreateYAFUser(string sUserName, string sPassword, string email)
{
if (!UserMembershipHelper.UserExists(sUserName, email))
{
YafMembershipProvider mb = (YafMembershipProvider)Membership.Providers["YafMembershipProvider"];
int? forumUserID = 0;
if (!mb.ValidateUser(sUserName, sPassword))
{
MembershipCreateStatus status;
MembershipUser forumUser = mb.CreateUser(sUserName, sUserName, sUserName, "question", "answer", true, null, out status);
// create the user in the YAF DB as well as sync roles...
forumUserID = RoleMembershipHelper.CreateForumUser(forumUser, 1);
RoleMembershipHelper.SetupUserRoles(1, sUserName);
RoleMembershipHelper.AddUserToRole(sUserName, "Registered");
// create empty profile just so they have one
YafUserProfile userProfile = YafUserProfile.GetProfile(sUserName);
userProfile.Homepage = "fwd.com";
// setup their inital profile information
userProfile.Save();
}
else
{
DataTable results = (DataTable)YAF.Classes.Data.LegacyDb.UserFind(1, false, sUserName, sUserName, sUserName, null, null);
//DataTable results = YAF.Classes.Data.DB.UserFind(1, false, sUserName, sUserName);
if (results.Rows.Count > 0)
{
forumUserID = (int)results.Rows[0]["UserID"];
}
}
}
}
#endregion
5. Fix login and register links in YAF or remove them completely.
Use something like this depending on your particular site structure for the YAF Links
https://localhost:44300/Account/VerifyCode/?provider=matt&returnUrl=%2Fforum%2FForum.aspx&rememberMe=true
and inside VerifyCode do a redirect to returnURL
Thanks Again!