Solved

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

Posted on 2008-06-24
5
1,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 74
Problem to Office 1 43
PowerShell: ForEach-Object Export to CSV 4 38
Office 365: Assigning MailboxPlan to a mailbox 8 6
A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

756 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