YAFLogo

Posted by: Demigod - Saturday, 9 February 2008 08:21:14
My forum currently uses v1.9.1 and I've integrated the security into my own website. I'm working on a huge redesign of the website and want to roll it out with v1.9.3+ but I'm having troubles following the logic when users login. Exactly which page or method is called when the user clicks "Forum Login"? Where is the cookie layed down? I want to recreate the login page so it looks like part of the site, not just part of the forum. In 1.9.1 I just used the user_login method, populated a bunch of session variables, and set the cookie with ForumsAuthentication. I've searched through the entire source of the current alpha and just can't find what I am looking for.

Posted by: Jaben - Sunday, 10 February 2008 16:04:04
v1.9.3 uses ASP.NET membership, roles, and profiles. Login is handled BY ASP.NET using a provider defined by you. For instance, you can tell YAF to use the Active Directory provider and "logging in" will be handled by this provider in it's unique way. YAF has it's own custom providers as well which are used for backwards compatiblity with v1.9.1.x and below. Without these providers you will not be able to upgrade an existing YAF configuration. Giving you want Active Directory -- v1.9.3 is probably the way to go. You can also do a search for "ASP.NET Membership Providers" for more details.

Posted by: Demigod - Sunday, 10 February 2008 18:45:47
I wasn't looking for AD.. I was just looking for a method where all of the actual logging in was done so I could add a few other session variables from the user object to the session so my other pages can use them. Gave up looking and went with a different approach. My site uses 3 different Master pages and in the Page_Init on each of them I test to see if "someone" is logged in.. and if so, grab what I need and populate the session vars. Is there a better way to accomplish the following code? [code] protected void Page_Init(object sender, EventArgs e) { if (HttpContext.Current.User != null && Session["UserName"].ToString() == "") { if (HttpContext.Current.User.Identity.IsAuthenticated) { if (HttpContext.Current.User.Identity is FormsIdentity) { string Username = HttpContext.Current.User.Identity.Name; string SQLConnection = ConfigurationManager.AppSettings["MySQL-String"]; string SQL = "SELECT u.Name, u.UserID, u.Level, u.ProviderUserKey FROM yaf_User u " + "WHERE u.Name = '" + Username + "'"; System.Data.DataTable UserInfo = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(SQLConnection, System.Data.CommandType.Text, SQL).Tables[0]; Session["UserID"] = UserInfo.Rows[0]["UserID"].ToString(); Session["Level"] = UserInfo.Rows[0]["Level"].ToString(); Session["UserName"] = UserInfo.Rows[0]["Name"].ToString(); Session["Chatter"] = new Chatter(new Guid(UserInfo.Rows[0]["ProviderUserKey"].ToString()), Session["UserName"].ToString()); } } } }[/code]

Posted by: Jaben - Monday, 11 February 2008 14:02:21
Just use Membership, Demigod: [code=cs] MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name); if (u != null) { // do some work } [/code]

Posted by: Demigod - Monday, 21 February 2011 05:31:11
[quote=Jaben;22316]Just use Membership, Demigod: [code=cs] MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name); if (u != null) { // do some work } [/code][/quote] 3 years later.. I still have questions on this. Now that I have actually upgraded to 1.9.5 I need to make this work. The User object you create there just doesn't contain any of the users info that I need.... like UserID. That's really the only useful item I need and every time I renovate my website I come back to this problem again. UserID is not part of that object. So... is there another way to access UserID, or is there a way for me to ADD UserID to that object? Just throw me a bone... I can handle the work.

Posted by: Demigod - Monday, 21 February 2011 08:56:31
Figured out a much easier way of dealing with this. I added this to my code: [code=csharp] using YAF.Classes.Core; [/code] And then this when I need to know the UserID of the user in question: [code=csharp] int UserID = YafContext.Current.PageUserID; [/code] If they're not logged in, it is 1. Else, they're logged in... then I can deal with whatever it was I needed to do.

Posted by: Jaben - Tuesday, 22 February 2011 02:38:32
If you needed the ID for any userName, you can use this: [code=csharp] var UserId = UserMembershipHelper.GetUserIDFromProviderUserKey(Membership.GetUser(userNameToLookup).ProviderUserKey); [/code]

Posted by: Demigod - Tuesday, 22 February 2011 04:46:56
Excellent stuff Jaben. Thanks.