Hi all, I have a login script that currently when a use logs in it works fine. i have set up an Admin role and have an Admin username. what i want to be able to do is when the Admin logs in. i want to redirect the admin to (~/Admin/Default.aspx)
protected void LoginUser_LoggedIn(object sender, EventArgs e)
{
string userName = (LoginUser.FindControl(“UserName”) as TextBox).Text;
//// or use
//// string userName = (e.Item.FindControl("UserName") as TextBox).Text;
MembershipUser user = Membership.GetUser(userName);
if (Roles.IsUserInRole(user.UserName, "User"))
{
// Do something and redirect to user page...
}
if (Roles.IsUserInRole(user.UserName, "Admin"))
{
// Do something and redirect to admin page...
}
}
Don't forget to click "Mark as Answer" on the post that helped you.
Thanks for the respone. Yeh i have read through a few of them. the problem i have is i have tried to incorporate it in my code and its not working for me. so i woulded if someone could fit that into my code to get it working.
Dino21
0 Points
10 Posts
Redirect user based on roles
Jan 04, 2013 05:14 PM|LINK
Hi all, I have a login script that currently when a use logs in it works fine. i have set up an Admin role and have an Admin username. what i want to be able to do is when the Admin logs in. i want to redirect the admin to (~/Admin/Default.aspx)
My login code atm is.
public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Page.Request.UrlReferrer != null) { Session["LoginReferrer"] = Page.Request.UrlReferrer.ToString(); } } if (User.Identity.IsAuthenticated) { FormsAuthentication.SignOut(); Response.Redirect("~/"); } } protected void LoginUser_LoggedIn(object sender, EventArgs e) { MyShoppingCart usersShoppingCart = new MyShoppingCart(); String cartId = usersShoppingCart.GetShoppingCartId(); usersShoppingCart.MigrateCart(cartId, LoginUser.UserName); if(Session["LoginReferrer"] != null) { Response.Redirect(Session["LoginReferrer"].ToString()); } Session["UserName"] = LoginUser.UserName; } }aabruzzese
Contributor
2806 Points
759 Posts
Re: Redirect user based on roles
Jan 04, 2013 05:35 PM|LINK
Hello Dino,
There are several other threads already present on the very same subject, but I do believe you can query the roles provider.
if (Roles.IsUserInRole(LoginUser.Username, "Administrator"))
Milind986
Participant
766 Points
161 Posts
Re: Redirect user based on roles
Jan 04, 2013 05:36 PM|LINK
try this
protected void LoginUser_LoggedIn(object sender, EventArgs e) { string userName = (LoginUser.FindControl(“UserName”) as TextBox).Text; //// or use //// string userName = (e.Item.FindControl("UserName") as TextBox).Text; MembershipUser user = Membership.GetUser(userName); if (Roles.IsUserInRole(user.UserName, "User")) { // Do something and redirect to user page... } if (Roles.IsUserInRole(user.UserName, "Admin")) { // Do something and redirect to admin page... } }My Blog
Dino21
0 Points
10 Posts
Re: Redirect user based on roles
Jan 04, 2013 05:38 PM|LINK
Thanks for the respone. Yeh i have read through a few of them. the problem i have is i have tried to incorporate it in my code and its not working for me. so i woulded if someone could fit that into my code to get it working.
aabruzzese
Contributor
2806 Points
759 Posts
Re: Redirect user based on roles
Jan 04, 2013 06:01 PM|LINK
Dino,
I could do some cut and paste here for you but I found a really good resource that might well help you get this resolved.
http://www.primaryobjects.com/cms/article97.aspx
Take a quick look and see if the answer is there as I suspect it is.
Also as some of the reponses to that article look interesting here is one:
if (Roles.IsUserInRole(loginControl.UserName, "Admin"))
{
loginControl.DestinationPageUrl = "~/Admin/Default.aspx";
}
Dino21
0 Points
10 Posts
Re: Redirect user based on roles
Jan 04, 2013 06:13 PM|LINK
Thanks for the reply.
I have tried the example on the link you give me it still not wroking correctly.
after logging in the admin is still be sent to the default.aspx page not the admin/default.aspx page
aabruzzese
Contributor
2806 Points
759 Posts
Re: Redirect user based on roles
Jan 04, 2013 06:34 PM|LINK
Try using this example as a seperate page all together.
<%@ Page Language="C#" %> <%@ Import Namespace="System.Web.Security" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> string[] rolesArray; MembershipUserCollection users; public void Page_Load() { Msg.Text = ""; try { if (!Roles.IsUserInRole(User.Identity.Name, "Administrators")) { Msg.Text = "You are not authorized to view user roles."; UsersListBox.Visible = false; return; } } catch (HttpException e) { Msg.Text = "There is no current logged on user. Role membership cannot be verified."; return; } if (!IsPostBack) { // Bind users to ListBox. users = Membership.GetAllUsers(); UsersListBox.DataSource = users; UsersListBox.DataBind(); } // If a user is selected, show the roles for the selected user. if (UsersListBox.SelectedItem != null) { // Bind roles to GridView. rolesArray = Roles.GetRolesForUser(UsersListBox.SelectedItem.Value); UserRolesGrid.DataSource = rolesArray; UserRolesGrid.DataBind(); UserRolesGrid.Columns[0].HeaderText = "Roles for " + UsersListBox.SelectedItem.Value; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Sample: View User Roles</title> </head> <body> <form runat="server" id="PageForm"> <h3>View User Roles</h3> <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br /> <table border="0" cellspacing="4"> <tr> <td valign="top"><asp:ListBox id="UsersListBox" DataTextField="Username" Rows="8" AutoPostBack="true" runat="server" /></td> <td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid" AutoGenerateColumns="false" Gridlines="None" CellSpacing="0" > <HeaderStyle BackColor="navy" ForeColor="white" /> <Columns> <asp:TemplateField HeaderText="Roles" > <ItemTemplate> <%# Container.DataItem.ToString() %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView></td> </tr> </table> </form> </body> </html>And if it does not show anything just remove the if condition.
oned_gk
All-Star
31017 Points
6352 Posts
Re: Redirect user based on roles
Jan 05, 2013 07:41 AM|LINK
In your default page page load event
if (User.IsInRole("admin")) { Response.Redirect("~/Admin/Default.aspx") }Dino21
0 Points
10 Posts
Re: Redirect user based on roles
Jan 05, 2013 10:15 AM|LINK
Thanks for both responces, I have tried them both and they dont seem to work for me.
aabruzzese
Contributor
2806 Points
759 Posts
Re: Redirect user based on roles
Jan 07, 2013 09:27 AM|LINK
Dino,
Just for the sake of checking, have you used the .net configuration tool to verify the users and roles?