Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Martijn de Wolf 11 posts 32 karma points
    Jul 10, 2012 @ 15:54
    Martijn de Wolf
    0

    Register Page Back-End User

    Hello,

    Ive tried searching the forum for this question but havent found any sollution so far.

    Is it possible to create a "front-end page" where a visitior can create a umbraco User account, so he can login to the back-end of umbraco. This "user" will have limited rights ofcourse. Im hoping that someone has a working example since Im more into the front-end development and xslt.

    Hope someone can help me.

    Greetings,

    Martijn

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jul 11, 2012 @ 04:17
    Tom Fulton
    0

    Hi Martijn,

    I don't have a fully working example, but you should definitely be able to do this using the API (via the User object).  Off the top of my head here is a quick example (not tested).

    // Get the UserType reference (built-in are Administrators, Writer, Editors)
    var userType = umbraco.BusinessLogic.UserType.GetAllUserTypes().FirstOrDefault(ut => ut.Name == "Editors");

    // Create the user
    var newUser = umbraco.BusinessLogic.User.MakeNew("name", "loginname", "password", "email", userType);

    // Set start nodes
    newUser.StartMediaId = -1;
    newUser.StartNodeId = -1;

    // Grant access to applications (sections)
    newUser.addApplication("content");
    newUser.addApplication("media");

    // Save
    newUser.Save();

    You'll of course need to create the form yourself, probably easiest to use Razor to do this.  You could probably also make this into a Contour workflowtype if that's the route you prefer.

    Also, I'm assuming you have a good reason to do this rather than using the Membership API and front-end pages, but if you think that would work for you it's probably a better option than giving visitors access to Umbraco...

    -Tom

  • Martijn de Wolf 11 posts 32 karma points
    Jul 11, 2012 @ 08:44
    Martijn de Wolf
    0

    Thanks Tom for the reply i will go try if i can make it work :)

    Cheers,

    Martijn

  • Martijn de Wolf 11 posts 32 karma points
    Jul 11, 2012 @ 12:50
    Martijn de Wolf
    0

    Hello Tom,

    I have one more question.
    I got it to work using a form. Only problem i am having now is that the password that gets send to the database doesnt work. I think this is because it doesnt get changed to a encrypted password. Any ideas how i can make this work ?

    Thanks,

    Martijn

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jul 11, 2012 @ 23:30
    Tom Fulton
    0

    Try using this:

    newUser.Password = "default";
    newUser.ChangePassword(newUser.Password, "newPasswordHere"); 

    -Tom

  • Martijn de Wolf 11 posts 32 karma points
    Jul 18, 2012 @ 16:29
    Martijn de Wolf
    1

    Thanks for your help Tom,

    I now have the most basic stuff working. And will continue to develope it further.
    Will post the code below if anyone is interrested :)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using System.Security.Cryptography;
    using System.Text;
    using umbraco;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.member;

    public partial class usercontrols_memberC_usercontrol : System.Web.UI.UserControl
    {
    // Error format
    private const string ErrorFormat = "<p class=\"formerror\">{0}</p>";
    string hashPassword(string password)
    {
    HMACSHA1 hash = new HMACSHA1();
    hash.Key = Encoding.Unicode.GetBytes(password);

    string encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
    return encodedPassword;
    }
    private static bool ensureUniqueLoginName(string loginName)
    {
    umbraco.BusinessLogic.User[] u = umbraco.BusinessLogic.User.getAllByLoginName(loginName);
    if (u.Length != 0)
    {
    return false;
    }

    return true;
    }
    protected void RegisterPlayer(object sender, EventArgs e)
    {

    if (ensureUniqueLoginName(tbUsername.Text))
    {

    // Get the UserType reference (built-in are Administrators, Writer, Editors)
    var userType = umbraco.BusinessLogic.UserType.GetAllUserTypes().FirstOrDefault(ut => ut.Name == "Editors");

    // Create the user

    var newUser = umbraco.BusinessLogic.User.MakeNew(tbName.Text, tbUsername.Text, hashPassword(tbPassword.Text), tbEmail.Text, userType);

    // Set start nodes
    newUser.StartMediaId = -1;
    newUser.StartNodeId = -1;


    // Grant access to applications (sections)
    newUser.addApplication("content");
    newUser.addApplication("media");

    // Save
    newUser.Save();
    litError.Text = string.Format(ErrorFormat, "Gebruiker is toegevoegd");
    }
    else
    {
    // Error, member already exists with email or username used
    litError.Text = string.Format(ErrorFormat, "Gebruiker bestaat al");
    }
    }
    }
Please Sign in or register to post replies

Write your reply to:

Draft