David H

David H

  • NA
  • 1
  • 3.1k

MVC 3 Windows authentication problems with redirect

May 11 2012 6:02 PM

I'm working on a MVC 3 project using Razor to render views. I'm deploying the build to a Windows Server 2008 R2 Enterprise box.

 

The application needs to take the current domain login for group management when accessing the views. The groups are managed in the configuration file of the application. During load of the controller action I get the user information and see if they are members of the group I created in my configuration file.

 

Partial code looks like thing

if (HttpContext != null)

{

ViewBag.UserName = HttpContext.Request.LogonUserIdentity.Name;

 

foreach (System.Security.Principal.IdentityReference group in HttpContext.Request.LogonUserIdentity.Groups)

{

userGroups.Add(group.Translate(typeof(System.Security.Principal.NTAccount)).ToString());

 

}

}

 

classObject.UserInGroup(ConfigurationManager.AppSettings["Administrators"], userGroups)

 

 

In my IIS Manager on the server I have the following setting for Authentication.

Anonymous: Disabled

ASP.Net Impersonation: Disabled

Forms Authentication: Disabled

Windows Authentication: Enabled

 

I know about the issue with MVC wanting to redirect to /Login page by default so I add the following two entries in to my app config section

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

    <add key="loginUrl" value="~/Redirect/Index" />

 

My authentication look like this.

    <authentication mode="Windows">

      <forms loginUrl="~/Home/Index" timeout="2880" />     

    </authentication>

 

My issue is as follows

 

Issue 1

 

The server is redirecting most of the time. Meaning instead of redirecting just on the first load of the site it redirects at random intervals and multiple times.

 

For example I call a Controller Action once using a <a href> on a page.

IE 9 will redirect multiples time for that one request.

 

I have a log file I generate that outputs a line every time a redirect happens. I call home controller once and the log generates the following information. It hits the redirect five times before going finally loading the view. Is there a configuration change I can make in the server or application so it only redirects once?

Controller,Action

Redirect,Index : URL: /

Redirect,Index : URL: /

Redirect,Index : URL: /

Redirect,Index : URL: /

Redirect,Index : URL: /

 

Issue 2

Because the redirect happens at intervals this is causing my partial views to fall.

 

I call the Controller index which loads the index view. The index view has jquery and JavaScript to load of a partial view called List in to a div in the index view. The list action requires parameters. Somethimes the process works great and other times the Authentication fires and routes thought the redirect causes the list view to be called without sending the parameters. This causes the page to stop since it couldn't get the action.


Examples

1)     Good flow

 

Controller,Action

D____,Index : Got here

D____,List : Var1: 0, Var2: 0

D____,List : Goto PartialView()

 

2)     Bad flow, Authentication happens

Redirect,Index : URL: / D____/Index/

Redirect,Index : URL: / D____/Index/

Redirect,Index : URL: / D____/Index/

Redirect,Index : URL: / D____/Index/

Redirect,Index : URL: / D____/Index/

D____,Index : Got here

Redirect,Index : URL: / D____/List/

 

Stops because Controller D____, List action is expecting two parameters

 

My Redirect Controller, Action looks like this

           public void Index()

               {

                    string returnURL = Request.QueryString["ReturnUrl"];

                    tracker.WriteSimpleLine("Redirect,Index", "URL: " + returnURL);

 

                     Response.Redirect(returnURL);

  }

 

 

Has anyone else experience this problem and know a solution?

 

 

 

v