473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GenericPrincipal without Forms Authentication

Greetings,

I need to implement GenericPrincipal based authentication without using
ASP.NET Forms Authentication.
I know it is much simpler using Forms Authentication, but in this case,
I have no control over the matter.

I have two pages - login.aspx and home.aspx.

Following is the Click event of the login button on the login.aspx page

protected void btnLogin_Click(Object sender, EventArgs e)
{
if(txtUserID.Text.Trim().ToUpper().Equals("USER1") )
{
buildSecurityContext("USER1");
Response.Redirect("Home.aspx");
}
else if(txtUserID.Text.Trim().ToUpper().Equals("User2") )
{
buildSecurityContext("USER2");
Response.Redirect("Home2.aspx");
else
{
lblMessage.Text = "Invalid User ID. Please re-enter.";
}
}

private void buildSecurityContext(string userName)
{
System.Security.Principal.GenericIdentity curIdentity = null;
System.Security.Principal.GenericPrincipal curPrincipal = null;
string[] roles = {"Role1","Role2"};
curIdentity = new System.Security.Principal.GenericIdentity(userName );
curPrincipal = new
System.Security.Principal.GenericPrincipal(curIden tity, roles);
HttpContext.Current.User = curPrincipal;
}

Following is the OnLoad event of the Home.aspx page

IPrincipal p = HttpContext.Current.User;
string userName = p.Identity.Name;
bool auth = p.Identity.IsAuthenticated;
bool isInRole = p.IsInRole("Role1");
lblUserName.Text = "Welcome " + userName + "<br>Your authentication
status is " + Convert.ToString(auth);
lblRoles.Text = "Your permission for Role1 is " +
Convert.ToString(isInRole);

The problem is that when the home page loads, the current request is
not authenticated.
At the end of the login process, the current identity is authenticated
and contains the correct user name and role.

But after the redirect to the home page, all that is getting lost
somehow.

What am I doing wrong?

Any help appreciated.

-Harold

Nov 19 '05 #1
3 3006
U should handle Application AuthorizeRequest event at global.asax. And
authorize request at this stage...

--
HTH

Thanks,
Yunus Emre ALPÖZEN
BSc, MCSD.NET

"Harold Crump" <or**********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Greetings,

I need to implement GenericPrincipal based authentication without using
ASP.NET Forms Authentication.
I know it is much simpler using Forms Authentication, but in this case,
I have no control over the matter.

I have two pages - login.aspx and home.aspx.

Following is the Click event of the login button on the login.aspx page

protected void btnLogin_Click(Object sender, EventArgs e)
{
if(txtUserID.Text.Trim().ToUpper().Equals("USER1") )
{
buildSecurityContext("USER1");
Response.Redirect("Home.aspx");
}
else if(txtUserID.Text.Trim().ToUpper().Equals("User2") )
{
buildSecurityContext("USER2");
Response.Redirect("Home2.aspx");
else
{
lblMessage.Text = "Invalid User ID. Please re-enter.";
}
}

private void buildSecurityContext(string userName)
{
System.Security.Principal.GenericIdentity curIdentity = null;
System.Security.Principal.GenericPrincipal curPrincipal = null;
string[] roles = {"Role1","Role2"};
curIdentity = new System.Security.Principal.GenericIdentity(userName );
curPrincipal = new
System.Security.Principal.GenericPrincipal(curIden tity, roles);
HttpContext.Current.User = curPrincipal;
}

Following is the OnLoad event of the Home.aspx page

IPrincipal p = HttpContext.Current.User;
string userName = p.Identity.Name;
bool auth = p.Identity.IsAuthenticated;
bool isInRole = p.IsInRole("Role1");
lblUserName.Text = "Welcome " + userName + "<br>Your authentication
status is " + Convert.ToString(auth);
lblRoles.Text = "Your permission for Role1 is " +
Convert.ToString(isInRole);

The problem is that when the home page loads, the current request is
not authenticated.
At the end of the login process, the current identity is authenticated
and contains the correct user name and role.

But after the redirect to the home page, all that is getting lost
somehow.

What am I doing wrong?

Any help appreciated.

-Harold

Nov 19 '05 #2
Yunus Emre ALPÖZEN [MCSD.NET] wrote:
U should handle Application AuthorizeRequest event at global.asax. And
authorize request at this stage...


Could you elaborate a little further....what code should I put in the
AuthenticateRequest event handler?
And how do I tie that in with the actual authentication check that is
currently being done in the login page button click?

Thanks,
Harold

Nov 19 '05 #3
try this out!

string username = "Anonymous";
string[] arrRoles = new string[1];
arrRoles[0] = username;
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(1,username,System.DateTi me.Now,
System.DateTime.Now.AddMinutes(20), false, username,
FormsAuthentication.FormsCookiePath);
HttpCookie cookie = new
HttpCookie(FormsAuthentication.FormsCookieName,
FormsAuthentication.Encrypt(ticket));

System.Security.Principal.GenericIdentity objIdentity = new
System.Security.Principal.GenericIdentity(username );
System.Security.Principal.GenericPrincipal objPrincipal = new
System.Security.Principal.GenericPrincipal(objIden tity, arrRoles);
_appContext.Response.Cookies.Add(cookie);
_appContext.User = objPrincipal;

"Harold Crump" wrote:
Yunus Emre ALPÖZEN [MCSD.NET] wrote:
U should handle Application AuthorizeRequest event at global.asax. And
authorize request at this stage...


Could you elaborate a little further....what code should I put in the
AuthenticateRequest event handler?
And how do I tie that in with the actual authentication check that is
currently being done in the login page button click?

Thanks,
Harold

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Harry Whitehouse | last post by:
I've got a fairly simple forms authentication project (a login page and a single secure page), but a number of users (~25%) can't seem to get past the login.aspx step. I theorized that this might...
3
by: Kris van der Mast | last post by:
Hi, I've created a little site for my sports club. In the root folder there are pages that are viewable by every anonymous user but at a certain subfolder my administration pages should be...
2
by: Eric | last post by:
I am trying to build an app where the stuff in the root directory is open to all, but anything under the Restricted directory requires you to login and I want to use Forms to do it. I'm having...
7
by: Justin | last post by:
I am trying to password protect a subdirectory using forms authentication. I am using the "Location" tag to specify the directory to be protected. The login.aspx page is in the root directory of...
5
by: V. Jenks | last post by:
Using forms authentication, can I control which pages and/or directories a user would have access to or is that only available with Windows authentication? Thanks!
2
by: pv | last post by:
Hi everyone, I need help with following scenario, please: Users are accessing same web server from intranet (users previously authenticated in Active Dir) and from extranet (common public...
2
by: Joyce | last post by:
Hi, Can I return GenericPrincipal from Web Services? The following error displays when I try to return GenericPrincipal from Web Services. System.Security.Principal.GenericPrincipal cannot be...
4
by: =?Utf-8?B?R3V1czEyMw==?= | last post by:
Hi, I created a web site on a remote server. To logon the user must enter a user id and password. The site is uses Forms Authentication. The web config file looks as follows: ...
1
by: DotNetNewbie | last post by:
Hi, When adding roles to the GenericPrincipal, is this a per request object or is it in session? In my global.asax.cs , which event should I be adding the roles to the GenericPrincipal?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.