Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# Shortie 4/26/2007 4:47 AM (edited 4/26/2007 12:49 PM)
How can I add a user in code? I try something like this:

public partial class PayPalTestUser : Cuyahoga.Web.Admin.UI.AdminBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
DefaultUserService _User = new DefaultUserService(userDao??, commonDao??);
string _password = _User.CreateUser("MailMe@hotmail.com","MailMe@hotmail.com",Site??);
Response.Write("User created: MailMe@hotmail.com | " + "Password: " + password);
}
I guess I have to use the DefaultUserService to do this but implementing this is bothering me. Any pointers are appreciated.

BTW I'm, using this inside a normal ASPX page (i've excluded this from the URL Rewriter).

Willem Jan
# Shortie 4/26/2007 1:29 PM
Tried this also:
public partial class PayPalTestUser : Cuyahoga.Web.Admin.UI.AdminBasePage
{
private Cuyahoga.Core.Domain.User _activeUser;

protected void Page_Load(object sender, EventArgs e)
{
// Generate password
string password = System.Web.Security.Membership.GeneratePassword(6, 1);

// Save user info
// Create a new user instance
this._activeUser = new Cuyahoga.Core.Domain.User();

this._activeUser.UserName = "MailMe@hotmail.com";
this._activeUser.Email = "MailMe@hotmail.com";
this._activeUser.Password = password;

Role role = (Role)base.CoreRepository.GetObjectById(typeof(Role), 3);
this._activeUser.Roles.Add(role);

base.CoreRepository.SaveObject(_activeUser);

Response.Write("User created: MailMe@hotmail.com | " + "Password: " + password);

}

Well I must be missing something. The error now: 'Unable to cast object of type System.Security.Principal.GenericIdentity to type Cuyahoga.Core.Domain.User'. AAny input would be very appreciated.

Willem Jan
# martijnb 4/27/2007 5:22 AM (edited 4/27/2007 7:35 AM)
First, the IUserService is still work in progress. There probably will be some extra overloads for CreateUser(), at least one that doesn't need a Site parameter (and you have to add the role(s) manually).

But something like this might work:
public partial class PayPalTestUser : Cuyahoga.Web.Admin.UI.AdminBasePage
{
  private IUserService _userService;
  
  public PayPalTestUser()
  {
    this._userService = Container.Resolve<IUserService>();
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    // Create a new user
    string name = "your_name";
    string email = "your_email";
    
    string generatedPassword = this._userService.CreateUser(name, email, base.ActiveSite);
  }
}

An alternative is to do it the old way. Check /Web/Admin/UserEdit.aspx for an example, but make sure to not use the CoreRepository anymore, but the I...Dao's instead.
# Shortie 4/27/2007 6:44 AM
Ha Martijn,

Thanks for the pointer. I tried it but I somehow got stuck at the container.Resolve part. I think it must be a Castle.Windsor container but I don't have one. Maybe I have to check this whole Castle thing a bit more.

Willem Jan
# martijnb 4/27/2007 7:37 AM
Originally, the plan was that services would be injected automagically, so you only need a setter property for the service in your code-behind. This didn't work as well as expected (memory leaks), so we now lookup the service via Container.Resolve<T>();
# Shortie 5/2/2007 7:18 AM (edited 5/2/2007 7:19 AM)
Somehow I cannot get this Container.Resolve to work. I would like to use the IUserService to check if a user is present in the database.

I would like to use:
this._userService.GetUserByUsernameAndEmail(string x, string y)

The Resolve is somewhere in the Castle definition but how do I get a correct reference to this? Hope this makes some sense.

BTW I insert a new user now via INSERT statements directly into the Cuyahoga tables. This works fine now. Ofcourse I can check with a SELECT statement but I think it is better to use this IUserService.
# martijnb 5/2/2007 4:14 PM
ShortieI would like to use:
this._userService.GetUserByUsernameAndEmail(string x, string y)

The Resolve is somewhere in the Castle definition but how do I get a correct reference to this? Hope this makes some sense.

The admin pages inherit indirectly from CuyahogaPage that has the reference to the container. If you want to access the container without using this inherited property, you can use Cuyahoga.Web.Util.ContainerAccessorUtil.GetContainer().
You'll need a reference to the Castle.Windsor assembly to make this work.