Link to home
Start Free TrialLog in
Avatar of MYTAIR
MYTAIR

asked on

How to login to a ASP.NET webpage automatically

Hi,
I need to figure out how to log a user in automatically when visiting an asp.net web page that requires forms authentication.

The case is:
I'm extending our corporate ASP.NET intranet site with a third party webapplication through an iframe. Our intranet uses windows authentication, but the page I'm accessing from the third party provider through the iframe, requires forms authentication. The credentials are however only the AD username as username and password.
How can I get the page to login automatically when a user visits the page witout redirecting to login.aspx?

I've tried some google searches but come up with nothing of use.

Regards
Jens
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Hi MYTAIR,
>>..How can I get the page to login automatically when a user visits the page witout redirecting to login.aspx?
You need to put one login page for Form authentication in web.config if the user not yet login through the system.
But, you may put some logic to bypass the login page that defined under other page that trying to invoke the page that need authentication.
eg:
                  //... server code that calling page that need authentication(eg: MemberTransactions.aspx), probably onClick event. You can apply it into your exact scenarios
                  FormsAuthentication.SetAuthCookie(YOURADUsername, false) ;
                  Response.Redirect("MemberTransactions.aspx", false);

Check this for further details:
http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.setauthcookie.aspx
Avatar of MYTAIR
MYTAIR

ASKER

Hi x com,
I've tried your solution:

FormsAuthentication.SetAuthCookie(YOURADUsername, false) ;
Response.Redirect("MemberTransactions.aspx", false);

but it just redirects me to the login page. It appears the cookie is not being set in my session.
Hi MYTAIR,
You can create the persist cookies there as described in the proposed MSDN site.
Response.Redirect("MemberTransactions.aspx", true);

Try delete the internet cache file and run the application again.
Lastly, post the code snippet here if the application still pointing to login page.

Avatar of MYTAIR

ASKER

Hi x com,
I've tried it again. See code snippet below.

It causes a redirect to itself, which will never end. That is when it redirects to CWP_Default.aspx, it finds out the user is not logged in, and redirects back to CWPLogin.aspx.


CWPLogin.aspx
 
<script runat="server">
protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
             
        string username = "72315"; 
        
        FormsAuthentication.SetAuthCookie(username, true);
        
          
        Response.Redirect("http://dk-ta-cphrm2/CWP_WA/CWP_Default.aspx", true);
 
}

Open in new window

Hi MYTAIR,
Sorry for the delay reply. May i see the code that resides inside CWP_Default.aspx? Could be some logic is causing the page redirection to Login page?
Avatar of MYTAIR

ASKER

Hi x com,
I wish I could, but I don't have access to the source code behind Login.aspx, because it's in a compiled dll.
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial