Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-24
5
Medium Priority
?
1,085 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses

885 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