Hola.
I'm in the early stages of creating a site which will solely be for logged in members. I've spent a good amount of hours trying to get the login functionality working.
I've tried the pre constructed macros in Umbraco the .NET membership class and now this:
I can see in the Members section in the backend that the test member is logged in by the "last login date" but I can't figure out how to check if the member is logged in persistantly across the pages.
Shouldn't I be able to do it this simply? Or is there something I'm totally missing?
If you are using Umbraco 7, you could take a look at the pre-defined Razor code snippets for a login form, and there is also a snippet that should give you the login status.
The pre-define snippet for getting login status looks like this:
@* Here you can specify a redirect URL for after logging out, by default umbraco will simply redirect to the current page. Example to redirect to the home page:
logoutModel.RedirectUrl = "/"; *@ }
@* NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed *@ @Html.RenderJsHere()
@if (loginStatusModel.IsLoggedIn) { <p>You are currently logged in as @loginStatusModel.Name</p>
using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout")) { <fieldset> <legend>Logout</legend> <button>Logout</button> </fieldset>
Yeah, as I wrote, I tried those already without luck. :/
I implemented the loginstatus macro in my master template and the login macro on my login page but added
@else { <p>You are not logged in </p> }
after
@if (loginStatusModel.IsLoggedIn){
<p>You are currently logged in as @loginStatusModel.Name</p>
using (Html.BeginUmbracoForm<UmbLoginStatusController>("HandleLogout"))
{
<fieldset>
<legend>Logout</legend>
<button>Logout</button>
</fieldset>
@Html.HiddenFor(m => logoutModel.RedirectUrl)
}
}
And consequently got the message: You are not logged in
I have a profile-editing page for a member. When the page loads, I know the member is logged in because when I set a breakpoint in the "RenderForm" code, I see that loginStatusModel.IsLoggedIn == TRUE and I am able to get their current member properties and populate the custom model for the form.
However, when I make a change to the form data and click the "submit" button, triggering the "HandleSubmit" code, loginStatusModel.IsLoggedIn == FALSE.
I just can't figure out why the member is getting logged-out on form submit. Am I missing something obvious here?
Here is a simplified version of my code:
MemberAccountForm Model
public class MemberAccountForm
{
public string EmailLogin { get; set; }
public string ContactName { get; set; }
public string CompanyName { get; set; }
... other properties
}
MemberAccountFormController
public class MemberAccountFormController : SurfaceController
{
[ChildActionOnly]
public ActionResult RenderForm(IPublishedContent CurrentPage)
{
...
var loginStatusModel = Members.GetCurrentLoginStatus(); <----- This has data & is TRUE
if (loginStatusModel.IsLoggedIn)
{
//Get the Current Member using MembershipHelper (cause it's faster)
var membershipUsername = Members.GetCurrentMember().GetPropertyValue("Username").ToString();
//Access Member using MemberService for property getting
var m = ApplicationContext.Services.MemberService.GetByUsername(membershipUsername);
var currMemProfile = new MemberAccountForm();
currMemProfile.CompanyName = m.GetValue<string>("companyName");
currMemProfile.ContactName = m.GetValue<string>("contactName");
currMemProfile.EmailLogin = m.Username;
currMemProfile.OriginalUserName = m.Username;
return PartialView("MemberAccountForm", currMemProfile);
}
else
{
return PartialView("MemberAccountForm", new MemberAccountForm { });
}
}
[HttpPost]
[ActionName("HandleSubmit")]
public ActionResult HandleSubmit(MemberAccountForm model)
{
if (ModelState.IsValid)
{
var loginStatusModel = Members.GetCurrentLoginStatus(); <!---- Now this is FALSE and all the values are NULL
if (loginStatusModel.IsLoggedIn)
{
...Do stuff to update & save properties & add "success" message to ViewData
}
}
else
{
//model not valid
//Do not save, but return current umbraco page
...Add model errors to ViewData
}
//Add current form model to ViewBag
ViewData["Model"] = model;
return CurrentUmbracoPage();
}
"MemberAccountForm" partial view
@using some stuff...
@model MemberAccountForm
@{
...Code to show errors & messages, if present in ViewData...
@RenderForm()
}
@helper RenderForm()
{
if (User.Identity.IsAuthenticated)
{
using (Html.BeginUmbracoForm("HandleSubmit", "MemberAccountForm"))
{
<label>
<span>Company Name</span>
@Html.EditorFor(x => Model.CompanyName, new { @type = "text" })
</label>
<label>
<span>Contact Name</span>
@Html.EditorFor(x => Model.ContactName, new { @type = "text" })
</label>
<label>
<span>Email Address</span>
@Html.EditorFor(x => Model.EmailLogin, new { @type = "email" })
</label>
..some other fields...
<button type="submit">Save</button>
}
}
}
Members.Login
Hola. I'm in the early stages of creating a site which will solely be for logged in members. I've spent a good amount of hours trying to get the login functionality working.
I've tried the pre constructed macros in Umbraco the .NET membership class and now this:
This basically writes out:
True
False
False
I can see in the Members section in the backend that the test member is logged in by the "last login date" but I can't figure out how to check if the member is logged in persistantly across the pages.
Shouldn't I be able to do it this simply? Or is there something I'm totally missing?
Cheers - Sune
Hi Sune,
If you are using Umbraco 7, you could take a look at the pre-defined Razor code snippets for a login form, and there is also a snippet that should give you the login status.
The pre-define snippet for getting login status looks like this:
Hope this can pull you in the rigtht direction.
/Dennis
Yeah, as I wrote, I tried those already without luck. :/ I implemented the loginstatus macro in my master template and the login macro on my login page but added
after
}
And consequently got the message: You are not logged in
Thanks for the quick reply though :)
I am having a similar issue.
I have a profile-editing page for a member. When the page loads, I know the member is logged in because when I set a breakpoint in the "RenderForm" code, I see that loginStatusModel.IsLoggedIn == TRUE and I am able to get their current member properties and populate the custom model for the form.
However, when I make a change to the form data and click the "submit" button, triggering the "HandleSubmit" code, loginStatusModel.IsLoggedIn == FALSE.
I just can't figure out why the member is getting logged-out on form submit. Am I missing something obvious here?
Here is a simplified version of my code:
MemberAccountForm Model
MemberAccountFormController
"MemberAccountForm" partial view
Any suggestions appreciated!
~Heather
is working on a reply...