Tags: belong, control, depending, event, javascript, loggedin, login, logs, net, pages, redirect, role, security, successfully, user

Using javascript in the LoggedIn event of the login control

On .Net » .Net Security

11,512 words with 4 Comments; publish: Sun, 06 Jan 2008 06:16:00 GMT; (10054.69, « »)

I am using a Login Control. After a user logs in, I am able to successfully redirect them to different pages depending on what role they belong to, using this code:

protectedvoid Login1_LoggedIn(object sender,EventArgs e){

Login _loginControl = (Login)Login1;

string _username = _loginControl.UserName;

if (Roles.IsUserInRole(_username,"Primary"))

{

Response.Redirect("PrimaryPage.aspx");

}

elseif (Roles.IsUserInRole(_username,"Secondary"))

{

Response.Redirect("SecondaryPage.aspx");

}

else

{

Response.Redirect("OtherPage.aspx");

}

}

However, what I would like to accomplish is to open a new browser window for the page they are being directed to and set the properties of that browser window. Some of the posts I've read suggest that I need to accomplish this by using Response.Write instead of Respons.Redirect, and pass in javascript, like this:

Response.Write("<script>window.open('TestPage.aspx','Test', 'width=500, height=600, menubar=no, toolbar=no, resizable=yes');</script>");

But I'm finding that this code does not work in the LoggedIn event, even though it works perfectly fine if I just throw a button on a blank web page and put it in the click event of that button. When I put it in the LoggedIn event, the user just gets redirected to the page identified in the DestinationPageURL property (which would be the proper response if I didn't have code to specifically redirect them to a different page).

So I guess I have 2 questions: 1) Does anyone know why this code doesn't work in the LoggedIn event even though it works elsewhere? And 2) Does anyone know a better method for accomplishing the goal of opening the page in a new browser window? (Or better yet, opening the page in the same browser window but turning off the browser toolbar, menubar, etc).

Any help would be appreciated.

All Comments

Leave a comment...

  • 4 Comments
    • Hi

      Here some example about how to call javascript function from codebehind in ASP.NET, Hope it helps

      http://www.codeproject.com/aspnet/Mayank_Gupta.asp

      This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
      #1; Sun, 06 Jan 2008 06:17:00 GMT
    • Hmm. I was aware of RegisterClientScriptBlock and RegisterStartupScript but don't think they will work for this scenario of redirecting users to new browser windows from the loggedin event.

      I was hoping that someone with more familiarity with the login control (and the loggedin event) might be able to answer this question. Specifically why my Response.Write code doesn't work in the LoggedIn event when it works fine elsewhere. This might give me the clue I need to successfully redirect users to a new browser window with no toolbars or menubars after logging in.

      #2; Sun, 06 Jan 2008 06:18:00 GMT
    • Hi

      I use the code like this, after user login, page will post back and you can open window base on the IsAuthenticated property.

      protected void Page_Load(object sender, EventArgs e)

      {

      if (Request.IsAuthenticated)

      {

      base.RegisterClientScriptBlock("", "<script>window.open('TestPage.aspx','Test', 'width=500, height=600, menubar=no, toolbar=no, resizable=yes');</script>");

      }

      }

      Hope it helps

      #3; Sun, 06 Jan 2008 06:19:00 GMT
    • Thanks XiaoYong Dai. This worked. I was able to add some code to re-direct the user to a different page based on what role they are in, like this:

      protectedvoid Page_Load(object sender,EventArgs e)

      {

      if (Request.IsAuthenticated)

      {

      string username = Context.User.Identity.Name;

      if (Roles.IsUserInRole(username,"Primary"))

      {

      base.RegisterClientScriptBlock("","<script>window.open('PrimaryPage.aspx','Test', 'width=500, height=600, menubar=no, toolbar=no, resizable=yes');</script>");

      }

      elseif (Roles.IsUserInRole(username,"Secondary"))

      {

      base.RegisterClientScriptBlock("","<script>window.open('SecondaryPage.aspx','Test', 'width=500, height=600, menubar=no, toolbar=no, resizable=yes');</script>");

      }

      else

      {

      base.RegisterClientScriptBlock("","<script>window.open('UnauthorizedPage.aspx','Test', 'width=500, height=600, menubar=no, toolbar=no, resizable=yes');</script>");

      }

      }

      }

      This code goes in the Page_Load event of the page that is specified in the DestinationPageURL attribute of the Login control. In my case, I made it the same page that contained the login control itself, but you can specify a different page. So it looks like I don't need to use the LoggedIn event after all.

      Thanks again.

      #4; Sun, 06 Jan 2008 06:20:00 GMT