I have a problem with ASP.NET in conjunction with Unity. Today the constructors of my controllers which require authenticated users have some parameters which can only be populated by Unity if the calling user is authenticated. If I call the URL which leads to such a controller UNAUTHENTICATED, the ASP.NET MVC pipeline tries to create the controller and Unit throws an exception because the required objects does not exist in the container. For the authentication I am using WSFederationAuthenticationModule and SessionAuthenticationModule and in the web.config I configured "Forms" as the authentication mode to force ASP.NET to redirect to the login page. I expected that the pipeline does not create a controller instance if the user is unauthenticated but redirects directly to the login page.
1
There are 1 best solutions below
Related Questions in AUTHENTICATION
- Access roles from multiple applications
- Different storyboard's entry points depending on a parameter
- SoundCloud Authentication Consistently Returns 401 invalid_grant For Some Users
- sendxmpp not authorized failure (Error AuthSend)
- Retrieve user information from Active Directory on login
- Log in through active directory
- Ember.js REST Auth Headers
- Validate Deezer access token on server
- Why does IIS Anonymous Authentication turn on by itself after I publish my project to server?
- Laravel - session data survives log-out/log-in, even for different users
- How can I share Azure Active Directory authentication between server side and client script?
- django rest framework - token authentication logout
- NameValuePair, HttpParams, HttpConnection Params deprecated on server request class for login app
- How to delete user from _User through Parse REST API
- Cannot login with new SQL User - SQL 2014
Related Questions in DEPENDENCY-INJECTION
- Resolve object using DI container with object instance
- Angularjs dependency injection parameter
- Dagger 2 - unable to inject object
- How to have SimpleInjector resolve viewmodel dependencies?
- Command Bus/Dispatcher and Handler registration without Dependency Injection
- Receiving a NoClassDefFoundError even though jar is successfully downloaded via Maven and referenced in pom.xml
- automapper error collection was modified when multiple users are creating a user
- When to use DI over abstract inheritance?
- Simple Injector Dependency Resolution Error - Could not load file or assembly System.Web.Http
- How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?
- Injecting login session using Dagger
- What's wrong with this factory dependencies issue?
- JAVA CDI: sometimes injection stays null when injected into EJB and interceptor in request scope
- Why a service of main module available in other modules?
- Can I specify multiple parameters using WhenInjectedInto for ninject?
Related Questions in ASP.NET-MVC-5
- Getting and passing MVC Model data to AngularJS controller
- How to fix the model to correctly use the MVC Foolproof library?
- Why do I have to publish MVC project twice?
- WebApi: Reading errors
- MVC route URL not containing parameter
- Checks not showing in the correct location
- Is there another way to unit test business logic in mvc
- Perform wildcard search of all (displayed) model fields in MVC?
- Identity 2.0 After Login go to Infinite Loop asp.net mvc 5
- How to define multiple partial Owin Startup classes and have them all run their code
- How to add asp.net mvc5 to visual studio express 2012 edition?
- Get desired html element's attribute value and set to hidden field before binding in Asp.Net MVC
- Authenticate User for ASP.NET MVC from Sharepoint
- MVC5 Typeahead Feature
- Validation for EditorFor helper mvc5
Related Questions in CONTROLLER-FACTORY
- How can I load multiple controller factories and pass control on to the next one?
- Custom ControllerFactory does not replace DefaultControllerFactory when there is a controller collision
- How do I get Web API / Castle Windsor to recognize a Controller?
- MVC 3 get ActionResult of an action that is defined on a different controller
- Resolving named registration using Unity 2 in DefaultControllerFactory
- If you are starting a new MVC3 project which adaption will you choose DependencyResolver or ControllerFactory with Castle Windsor?
- Issue with passing constructor parameter via controller factory
- Modify ControllerContext in CreateController method of DefaultControllerFactory in asp.net core mvc
- asp.net mvc controller factory
- Best place to set CurrentCulture for multilingual ASP.NET MVC web applications
- ASP.NET MVC creates controllers which require authentication although the user is unauthenticated which leads to a dependency injection issue
- MVC3 / Structure Map 2.6.2 DI custom controller factory problem
- ControllerFactory for specific portable area
- what exactly ObjectFactory is, and, whats is it used for?
- Seems No Controllers Recognized by my controller factory
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
That's the root of your problem. This means that the constructor of that service does too much. Constructors should not do more than store the incoming dependencies. This way you can compose object graphs with confidence.
The building of object graphs should in general be static. This means that the resolved object graph should not change based on runtime conditions (there are exceptions, but this is a good rule of thumb). It shouldn't matter whether or not a user is authorized or not, the service class should still be composed and injected. This means that authorization and loading of data is done at a later point in time; the moment that the request is actually executed (which is directly after the object graph is composed).
So instead of doing this work in the constructor, move this work to the moment that the action method is executed. You can move this logic to a service that does the authentication, use a filter attribute, or implement this using a decorator.