0

I am using c# membership provider and I get the username from a query string. Now I need to check if the username exist if it does I need to automatically authenticate the user.

How do I check if the user exists in the membership database?

3 Answers 3

2

If you have a password:

 if (Membership.ValidateUser(userName, "password")) 
 {
     FormsAuthentication.SetAuthCookie(userName, true);
     Response.Redirect("~/welcome.aspx");
 }

or if you just want to check if the user exist and log them in

 if (Membership.GetUser(userName) != null) 
 { 
     FormsAuthentication.SetAuthCookie(userName, true); 
     Response.Redirect("~/welcome.aspx");
 }
Sign up to request clarification or add additional context in comments.

Comments

1

If you are looking for an SSO solution, you can find more information here

http://weblogs.asp.net/hernandl/archive/2004/06/09/ssoformsauth.aspx

3 Comments

i am looking for an SSO solution but the 2 application are using different databases and the 2 projects are under the same solution
Then you need to pass encrypted username with an expiring timestamp to the second server and authenticate the user there using the technique explained in the above link.
if (user != "") { FormsAuthentication.SetAuthCookie(user, false);} i did this it worked but i need to refresh the page for the cookie to take effect and authenticate the user
1

Check the Membership.GetUser method. If the user exists, you can then use FormsAuthentication.SetAuthCookie to authenticate the user.

2 Comments

ok i got the user information and i found him in my database and everything is ok but how to authenticate him ? i mean how i can make him login to the site and display the msg for example : welcome ...
I amended the answer to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.