Solved

ASP.NET 4 and visual studio 2010 Membership web

Posted on 2011-02-16
6
539 Views
Last Modified: 2013-11-26
Hello.  i am new to ASP.net programmng.  I am using visual studio 2010 and created a website using asp.net 4 and vb.  The website installed the default login sceem and allows me to create roles.  What I want to have the registered user directed to a specific page built for them based on their user id.    Can anyone point me in the right direction to get this done.  So far I create roles for each user and allowed only the user assigned to the role access to the page i want them to go.   But everytime a user logs in it automatically directs them to the default.aspx page.

Thank you
0
Comment
Question by:bidgadget
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34915293
Hi,

You have a couple of options:

1) In the Page_Load event (or Page_Init) for Default.aspx, add some logic for the conditional redicrection - or -
2) For the Login control, specify the "DestinationPageUrl" property to something like "Redirect.aspx" and put some code in there:
Partial Class Redirect
    Inherits System.Web.UI.Page


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If Not Page.IsPostBack Then
            If User.Identity.IsAuthenticated Then
                For Each role As String In Roles.GetAllRoles()
                    If User.IsInRole(role) Then
                        Response.Redirect("~/" & role.ToString() & ".aspx")
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub
End Class

Open in new window


Assuming you have role names matching page names this little piece of code will automatically redirect the user to the correct page. Let's say you have a role called "admins" - if you have a page called "admins.aspx" any user in that group will be directed to it. You will need to change this logic to suit your needs but you should get the idea.

/Carl.
0
 

Author Comment

by:bidgadget
ID: 34915545
Thank you. Quick question and please forgive my lack of experience.  When you say in the login control, do you mean the login.aspx page.  And where do I specifily the destination URL.

Thank you for responding
0
 
LVL 18

Accepted Solution

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

In your login page (presumably Login.aspx) you will have a server control like:

<asp:Login ID="Login1" runat="Server">
</asp:Login>

Either you select the control in the IDE and scroll through the properties window until you find the "DestinationPageUrl" property or simply add the property to the declarative markup like:

<asp:Login ID="Login1" runat="Server" DestinationPageUrl="~/Redirect.aspx">
</asp:Login>

Please note, the declarative markup for your control might contain other properties other than what are shown here.

/Carl.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:bidgadget
ID: 34915883
Sorry.  One last question.  I was able t add the redirect.aspx  your explaination was perfect.  On the redirect.aspx I am not sure where to add the code

Partial Class Redirect
    Inherits System.Web.UI.Page


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If Not Page.IsPostBack Then
            If User.Identity.IsAuthenticated Then
                For Each role As String In Roles.GetAllRoles()
                    If User.IsInRole(role) Then
                        Response.Redirect("~/" & role.ToString() & ".aspx")
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub
End Class
0
 

Author Comment

by:bidgadget
ID: 34916106
I found it.  works perfect.  thans a million
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 34916211
Hi,

You're welcome!

/Carl.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

930 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now