I am trying to setup simple membership on an existing website I've just built using WebMatrix. I've followed the steps in this tutorial - http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=2240
The problem I have is that after login if I call WebSecurity.IsAuthenticated it never seems to actually be logged in. The code on the login gets as far as the redirect so one would assume the user has been authenticated. Here is my login code:
@{
var username = "";
var password = "";
var errorMessage = "";
if(IsPost){
username = Request["username"];
password = Request["password"];
if(WebSecurity.Login(username,password,true)){
Response.Redirect("~/admin/modules/pages");
}
else
{
errorMessage = "Login was not successful.";
}
}
}
When the user is redirected to the /admin/modules/pages location there is a simple piece of code to show the username if logged in but it never does. It just states that you are not logged in. Here's that code:
@if(WebSecurity.IsAuthenticated)
{
<h2>Hello @WebSecurity.CurrentUserName, you are logged in | <a href="/admin/logout">Log out</a></h2>
}
else
{
<h2>You are not logged in | <a href="/admin/login">Log in</a></h2>
}
Hope someone can help. Thanks in advance.
This is strange and shouldn't be happening. Try matching your code with the samples in WebMatrix. Also match your webconfig file with the sample's webconfig. Something must be wrong someplace.