开发者

ASP.net MVC application logging out very quickly after logging in

开发者 https://www.devze.com 2023-02-26 01:36 出处:网络
This is probably a very simple problem but I cant see the solution and its really beginning to bug me!

This is probably a very simple problem but I cant see the solution and its really beginning to bug me!

I have a simple ASP.Net MVC2 application which is intended as a learning aid which requires users to log in by providing username and password. These are authenticated against a DB using a membership provider was created and configured using the aspnet_regsql tool.

The web config looks like this (which I think is right... maybe not sure about the forms authentication bit however):

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2380"/>
</authentication>
<membership>
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
</profile>
<roleManager enabled="false">
  <providers>
    <clear />
    <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
  </providers>
</roleManager>

The code in the view which performs the log procedure looks like this:

public ActionResult LogOn()
{
    return View();
}

[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid){
        if (MembershipService.ValidateUser(model.UserName, model.Password)){
            FormsService.SignIn(model.UserName, model.RememberMe);
            if (!String.IsNullOrEmpty(returnUrl)){
                return Redirect(returnUrl);
            }
            else{
                return RedirectToAction("Index", "Home");
            }
        }
        else{   
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

And each method in the controller which requires security applied is tagged like so:

[Authorize]
public ActionResult New() {
  return View();
}

[Authorize]
[HttpPost]
public ActionResult New(FormCollection collection) {
    //d开发者_运维问答o something...
}

I have also tagged the controller class with [Authorize] also.

However, when I log in to the site by creating a new item or editing an existing item I get logged out very quickly after logging in.

Do private methods in the controller also need to marked with [Authorize] and would this cause someone to get logged out?

Thanks for your help,

Morris

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号