I am trying to extend the user identity in front office of Umbraco with the following tutorial.
This makes it possible to do the following thing in Razor: "@((User as CustomPrincipal).FirstName)", so you can access custom fields in the front office after you are logged in with a custom form, which works perfectly
But when I am logged in in the front office with a custom login and now want to login in the back office with the right credentials it redirects you to a blank page and you are not logged in, so you can't edit any page. It does not give you any error or something.
Do you extend the member or User? In umbraco you have a member that is a frontend user, and Users are the backend users.
It seems like you trying to add fields to the frontend member. I would suggest you do this by going to members in backend, add a property to the memberType, or add a new memberType if the properties should only apply to some.
Yes, I want to extend the Umbraco member. But I am logged in using a Active Directory and not the Memberprovider.
The problem is somewhere over here:
using eFocus.Peevee.Models;
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using Umbraco.Web;
namespace Umbraco.Extensions.Utilities
{
public class Global : UmbracoApplication
{
/// <summary>
/// With this function we will extend the current principal by our own custom model.
/// This makes it possible so you can use @User.Firstname in Razor as example. See the
/// available variables in Models/MemberModel.cs
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
// First we will check someone is FormsAuthenticationed.
if (authCookie != null)
{
// Then we will decrypt the cookie because it was encrypted at login.
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
// Now we will parse the userData that was serialized back to an object.
JavaScriptSerializer serializer = new JavaScriptSerializer();
MemberModel serializeModel = serializer.Deserialize<MemberModel>(authTicket.UserData);
// We will fill our own custom principal with the information from the cookie.
CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.Name = serializeModel.Name;
newUser.Firstname = serializeModel.Firstname;
newUser.Lastname = serializeModel.Lastname;
newUser.Mail = serializeModel.Mail;
newUser.Company = serializeModel.Company;
// Here we bind the custom principal to the HttpContent.
HttpContext.Current.User = newUser;
}
}
}
}
Now for Members, visitors to the site, there IS a bit of a proof of
concept for using ASP.Net Identity for members in Umbraco, core member
Shannon has a repo on GitHub here:
I have fixed it by not overwriting the HttpContext.Current.User but saved it to a item using: HttpContext.Current.Items["IUser"] = newUser;. A little bit dirtier but it works! :)
In v8 is there any way or a better way to achieve what the OP was trying to do?
I was doing the following:
IMyCustomPrincipal newUser = new MyCustomPrincipal(authTicket.Name);
if(userData != null)
{
newUser.Id = userData.Id;
newUser.Name = userData.Name;
newUser.TenantReference = userData.TenantReference;
}
// Set the current user to our custom IPrinciple
HttpContext.Current.User = newUser;
But setting the HttpContext.Current.User obviously destroys any back-office session you might have so you can either be logged in to the front end or the back end but not both.
Extend HttpContent.Current.User
Hi,
I am trying to extend the user identity in front office of Umbraco with the following tutorial.
This makes it possible to do the following thing in Razor: "@((User as CustomPrincipal).FirstName)", so you can access custom fields in the front office after you are logged in with a custom form, which works perfectly
But when I am logged in in the front office with a custom login and now want to login in the back office with the right credentials it redirects you to a blank page and you are not logged in, so you can't edit any page. It does not give you any error or something.
Someone knows what the problem is?
Kind regards,
Yanick
Do you extend the member or User? In umbraco you have a member that is a frontend user, and Users are the backend users.
It seems like you trying to add fields to the frontend member. I would suggest you do this by going to members in backend, add a property to the memberType, or add a new memberType if the properties should only apply to some.
Read more about member types here: https://our.umbraco.org/documentation/getting-started/data/members/
Hi Jan,
Yes, I want to extend the Umbraco member. But I am logged in using a Active Directory and not the Memberprovider.
The problem is somewhere over here:
Can you help me out? Still haven't fixed this :(
Can't say I've worked with extending Umbraco membership provider.
Found this: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/76777-member-ad-authentication-not-logging-in-member#comment-245396
Note that if you're running umbraco before version 7.4.2 you need to use older version of the package. Maybe you can find some answers there?
I have fixed it by not overwriting the HttpContext.Current.User but saved it to a item using: HttpContext.Current.Items["IUser"] = newUser;. A little bit dirtier but it works! :)
In v8 is there any way or a better way to achieve what the OP was trying to do?
I was doing the following:
But setting the
HttpContext.Current.User
obviously destroys any back-office session you might have so you can either be logged in to the front end or the back end but not both.is working on a reply...