Adding temporal role

A

aitor

Hello,

I am doing a management of roles for users. At the beginning every user
will have only one role. I haven´t used role provider because I don´t want
to add a new record into the database. I just want to add roles and when I
restart the application, with the global.asax file will be assigned roles
that I had at the beginning. How can I do to add roles during the execution?

Using Roles.AddUserToRole() function doesn´t work because role provider is
disabled.

I use this sentence in Global.asax:

string[] difRoles = new string[1] { "administrator" };


HttpContext.Current.User = new
System.Security.Principal.GenericPrincipal(User.Identity, difRoles);

But if I use this sentence in execution is not working.

Do you know what sentence should I use to get what I want?

Thank you.
 
G

Guest

Hello,

 I am doing a management of roles for users. At the beginning every user
will have only one role. I haven´t used role provider because I don´twant
to add a new record into the database. I just want to add roles and when I
restart the application, with the global.asax file will be assigned roles
that I had at the beginning. How can I do to add roles during the execution?

 Using Roles.AddUserToRole() function doesn´t work because role provider is
disabled.

 I use this sentence in Global.asax:

                string[] difRoles = new string[1] { "administrator" };

                HttpContext.Current.User = new
System.Security.Principal.GenericPrincipal(User.Identity, difRoles);

But if I use this sentence in execution is not working.

Do you know what sentence should I use to get what I want?

Thank you.

I think the code is correct. Where do you call it, I suppose in
Application_AuthenticateRequest method?

Try to check after that the roles

string userData = ticket.UserData;
string[] roles = userData.Split(',');
foreach(string role in roles)
{
Response.Write("You are: " +role.ToString()+"<BR>");
}

or simply

if (User.IsInRole("administrator"))
{
.....
}

More about FormsAuthentication
http://www.eggheadcafe.com/articles/20020906.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top