Link to home
Start Free TrialLog in
Avatar of shanemay
shanemayFlag for United States of America

asked on

Prgrammatically log a user into a secure site: ASP.NET

I would like to make a programmatic FORM post containing the username and password, which would be the programmatic version of a user filling in the fields and pressing the LOGIN button.  I know that it can be done with the WebClient class in System.Net namespace, and that I would need to capture any cookies that are set and return them with any subsequent request, and the "page" that results will come back from the WebClient action.  I am not sure where to start to make it happen.  

Any help would be greatly appreciated.  

Thank you.
Avatar of lotusnotesnewbie
lotusnotesnewbie
Flag of India image

I am not sure if you asking for a Login from with Remember me functionality. Kindly clarify.
Avatar of shanemay

ASKER

Thank you for the response, I am storing the username and password for different sites, and I would like a user to click a button and I take the username and password and programmatically log them into a site.  Kind of like a Single Sign on Function.

So if the user clicks the email button, I would log them into the email site, if they click hr, I would log them into the hr site. I have the users information I am just not sure how to accoplish this.  Each site has its own set of authentication.  I am able to programmactically recteate the login form and submit it, however, that requires me to place there username and password on the webpage in plain text which I do not want to do.  

 
Ok, what i understand is that you wants to post the values to another page instead to process them on the first page.

if i am correct then comment back.

also may be these links will be useful

http://www.beansoftware.com/ASP.NET-Tutorials/Cross-Page-Posting.aspx

http://www.jigar.net/articles/viewhtmlcontent78.aspx

http://www.stardeveloper.com/articles/display.html?article=2003061901&page=1

http://www.codeproject.com/KB/aspnet/PostToAnotherPage.aspx
Has far has i know, if you are using built-in authentication from asp.net 2.0 or over, all use info should be stock in 1 database with a colum name .Application. that correspond witch site application user has access.

From their, each web application has is own web.config file with membership provider configurations.
When you use the "MembershipProvider" it is authaumaticaly bind to the current application where the code is executed.
This way you can use MembershipProvider.ValidateUser.

But in your case you want from "application 1" log on the user into the "application 2".
Don't think this can be done by default you will have to write your own "authentication mecanism".

But if you need to manually validate user authentication and the log them into web site you can use this.

MembershipProvider.ValidateUser
FormsAuthentication.RedirectFromLoginPage
FormsAuthentication.RedirectToLoginPage

Check this out
http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication_members.aspx
http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.aspx
http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx

Let us know
Something like this
----------------------
if(MembershipProvider.ValidateUser(userNameVar,passVar))
   FormsAuthentication.RedirectFromLoginPage(userNameVar,createCookiesBool);

Open in new window

ajaysharmaapjs,  You are correct, I have a user that authenticates against my active directory, then stores usernames and passwords for our other services that do not authenticate against active directory.  For example, our hr site does not authenticate against AD.  Therefore, a user would login to the site, save the username and password for the hr site in our database, then when the user clicks the hr button, I would take the credentials stored in the database and programmatically log them into the hr site.  I have seven services that I need to do this with.

Hope that helps clarify what I am trying to accomplish.  

Thank you for your help.  
Maybe to clarify a bit more what I would like to accomplish.  I would like to make a programmatic FORM post containing the username and password (and possibly other fields), which would be the programmatic version of a user filling in the fields and pressing the LOGIN button.
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada 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
Thank you for the code sample, I believe I should be able to adapt this and make it work.