21

I have an ASP.NET MVC 3 Beta application running on IIS. In my web.config I defined following section responsible for forms authentication:

<authentication mode="Forms">
    <forms 
        loginUrl="~/Account/LogOn" 
        name=".VNK" 
        protection="All" 
        timeout="43200" 
        cookieless="UseCookies" />
</authentication>

The defined login address is ~/Account/LogOn.

When I try to get the login url using:

FormsAuthentication.Initialize();
string loginUrl = FormsAuthentication.LoginUrl; 

I receive: /VNK/site/Account/Login

Why do I get a different address from the one defined in web.config?

UPDATE: The "/VNK/site/" prefix is not a problem here. The problem is that LoginUrl property of FormsAuthentication class does not reflect the value from web.config. It means that if I change the value of loginUrl attribute in web.config from "~/Account/LogOn" to e.g. "~/foobar", FormsAuthentication.LoginUrl still has value of "/VNK/site/Account/Login". Why ?

3
  • Try removing the period from your name attribute ie. name="VNK"
    – Jimmy
    Nov 3, 2010 at 14:38
  • @Jimmy - the name attribute has no influence for that issue at all. The name attribute just sets the suffix for the name of the cookie that contains the authentication ticket.
    – jwaliszko
    Nov 3, 2010 at 15:00
  • I know, just taking shots in the dark as everything else looks good
    – Jimmy
    Nov 3, 2010 at 15:24

8 Answers 8

27

I think there is a bug in ASP.NET MVC 3 Beta. This problem does not appear in previous releases of ASP.NET MVC.

If anyone wants to replay this error, he should follow this:

1.Download the mvc framevork.

2.Create new ASP.NET MVC 3 Web Application

3.Applay Authorize attribute on About action in HomeController

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

4.Start application and invoke About action by clicking on About tab. You will get server error, because application is trying to redirect You to such URL:

http://localhost:[port_num]/Account/Login?ReturnUrl=%2fHome%2fAbout

There is obviously no Login view. There is LogOn view. Url to LogOn action is defined in untouched web.config:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>

But application does not reflect that. Have anyone any clue what's going on ?

UPDATE:

I was right, there is a bug in MVC 3 Beta. From known issues:

"There’s a known issue that causes Forms Authentication to always redirect unauthenticated users to /Account/Login, ignoring the forms authentication setting used in Web.config. The workaround is to add the following app setting."

<add key="autoFormsAuthentication" value="false" />

UPDATE 2:

Alexander Prokofyev noticed, that ASP.NET 3 RTM looks for another setting. So you need this line instead:

<add key="loginUrl" value="~/LogOn" />

3
  • 22
    ASP.NET 3 RTM looks for another setting. So you need this line instead: <add key="loginUrl" value="~/LogOn" /> Mar 18, 2011 at 12:58
  • @Jarek Waliszko please update your answer with the comment above - I almost missed it.
    – Alex
    May 8, 2012 at 12:03
  • There's an additional setting you can use with MVC 4 <add key="PreserveLoginUrl" value="true" /> which makes it use the forms auth setting Jan 15, 2013 at 12:29
1

If you have access to IIS, then append a new application and enable ASP.NET "integrated pipelining" in application pool section by double clicking it.

If your hosting provider does not grant you access to IIS, then login to the control panel.

  • Go to websites, under the management tab- enable ASP.NET integrated pipe lining.
  • Set your application as a virtual directory (It worked for me)
1

So the simple solution was to remove WebMatrix.*.dll from Bin folder in web project. I have done this for my asp.net project since it was redirecting my login to mvc style url.

1
  • you should remove the nuget package. i had one of my team member deleted the dll. but then we migrated the package to latest version and that dll came back in which caused the issues again
    – Hrishi
    Aug 3, 2016 at 9:55
0

Updated answer for MVC 4, heavily borrowed from this page and Request redirect to /Account/Login?ReturnUrl=%2f since MVC 3 install on server

<appSettings>
   ...
   <add key="PreserveLoginUrl" value="true" />
</appSettings>

...

<authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="43200" /> <!--43,200 in minutes - 30 days-->
</authentication>
0

Put the following in appSettings:

<add key="loginUrl" value="~/Account/LogOn" />

You could empty loginUrl from Form Authentication configuration.

0

I removed the following from my web.config

<remove name="FormsAuthentication" />

and then everything seemed to work. This text had been added by default when I created my project.

2
  • I assume you are aware that this question has been asked 6 yers ago and refers to ASP.NET MVC 3 Beta.
    – jwaliszko
    Mar 18, 2016 at 15:14
  • 1
    Yes, of course. But I had the problem on MVC 4 a week ago, and when googling for an answer, this page still came up as one of the best hits. So it seemed like a good place to post my solution in case anyone else was also still experiencing problems.
    – ChrisFox
    Mar 20, 2016 at 16:59
-1

The tilde (~) means "the root of my web site" so you don't have to keep using .. or \ to step up and down the web site structure. However, from an IIS perspective you web application may have an additional layer of directory structure which is being reflected when you request the LoginUrl programmatically. I'm unsure as to why you want to retrieve the LoginUrl, the normal state of affairs would have IIS redirect the user to the LoginUrl automatically any time they try to access a page that they are not authenticated for.

2
  • Hey, I know what is (~), the problem is my FormsAuthentication.LoginUrl does not reflect loginUrl from web.config. If I change in web.config loginUrl from "~/Account/LogOn" to "~/foobar", FormsAuthentication.LoginUrl still has value of "/VNK/site/Account/Login". Why ?
    – jwaliszko
    Nov 3, 2010 at 14:09
  • Is your project named VNK? and running locally?
    – Jimmy
    Nov 3, 2010 at 14:35
-1

I think the server has trouble deciding what ~ means in this case, try giving a more direct url to the login page, such as /Account/LogOn.

Maybe you'll find something useful here http://msdn.microsoft.com/en-us/library/xdt4thhy.aspx

1
  • Hey, it's not the problem of (~). The problem is loginUrl is not reflected at all. If I change the timeout, FormsAuthentication.Timeout is changing, but when I change loginUrl, FormsAuthentication.LoginUrl stays untouched.
    – jwaliszko
    Nov 3, 2010 at 14:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.