Solved

Page redirect baste on membership role. (asp.net 2.0 and c#)

Posted on 2008-06-24
5
1,076 Views
Last Modified: 2013-12-16
I have 2 member ship roles in my site, directors and staff.  After a user logs in, I would like to have a page load event, that would redirect the directors to a different  start page.  Can someone show me how i can do this with a simple if statement, in the page load event?  Im working with ASP.Net 2.0 and C#.

Thanks for any help.
0
Comment
Question by:bmanmike39
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21856180
Does this help:
Response.Redirect("http://your_url_here");
0
 

Author Comment

by:bmanmike39
ID: 21856287
I understand the response redirect,  I dont know how to write the code, for the user.name.identity. in other words,

If  userneme = = directors
{
response.redirect(this url)
}

Something like the above.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 21856439
Hi, do you mean how to check if the membership role equals certain value? It depends on what data type do you use to store the membership roles. Typically you would use integer, then the code would be like:

if (membershipRole == 0)  // let's say director
  Response.Redirect(... /* director page */);


otherwise, if it is a string:
if (membershipRole.Equals("Director"))
  Response.Redirect(... /* director page */);

The value of membershipRole should be stored somewhere in database, eg:

user_id | login_name | password | membership_role
------------------------------------------------------------------
1           | abc              | abc123     | 0
2           | def              | def123      | 1
...

You need to use select query to get that based on the login name the user supplied
0
 

Author Comment

by:bmanmike39
ID: 21856676
Im using the Asp.net 2.0 membership provider, and i have the roles turned on.  I need to get the role of the user from the provider. After the login page redirects to the start page.

I know how to get the users name for the provider, it User.Identity.Name but how do i get the users Role from the provider?
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 21865174
Hi,

I'm really a VB guy but I will give it a go in C# as well, please see the attached code snippets..

/Carl.
'VB.NET Version
 
Protected Sub Page_Load(ByVal sender as Object, ByVal e As System.EventArgs) Handles Me.Load
	If User.Identity.IsAuthenticated Then
		If User.IsInRole("directors") Then
			Response.Redirect("~/secure_directors/Default.aspx") 'Your url by choice..
		End If
		If User.IsInRole("staff") Then
			Response.Redirect("~/secure_staff/Default.aspx")
		End If
	End If
End Sub
 
'C# Version
 
protected void Page_Load(object sender, System.EventArgs e) {
    if (User.Identity.IsAuthenticated) {
        if (User.IsInRole("directors")) {
            Response.Redirect("~/secure_directors/Default.aspx"); //Your url by choice..
        }
        if (User.IsInRole("staff")) {
            Response.Redirect("~/secure_staff/Default.aspx");
        }
    }
}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question