I was wondering how you could mock that a controller is decorated with the [Authorize] attribute in MVC3? I am using a custom membership provider. I would like to test that a controller been decorated with the attribute and you are authorized and what happens when you are not. I'm using Moq. Any good suggestions on this?!
UPDATE:
I'm currently getting an NullreferenceException saying "object reference not set to an instance of an object". It's the same error as previously mentioned in this post NullReferenceException while using Authorize Attribute
This is related to the Authorize attribute. I'm running under iis and not using cassini. Does anyone know if this is somehow related to applicaton pool and user rights. The tests that I have for the authorize attribute wont work until this is fixed.
Maybe there is some other way of testing the Authorize attribute? Big thanks in advance.
UPDATE2
So after some extensive research and debugging help from a colleague I mentioned to fix the problem with the [Authorize] attribute. It appears as though this line in the web.config was missing:
<modules runAllManagedModulesForAllRequests="true">
Strangely enough this did not cause problem for another colleague that was sharing the trunk or in beta environment. It only caused problems for me locally. This might have been related to something in the GAC. Anyhow all works now.
Thanks.
I wrote a blog post about exactly that a couple of months ago:
http://thomasardal.com/unit-testing-attribute-decorations/
Related
I'm working on a web application but it seems have a problem but I can't figure it out whats wrong, and yes there are so many questions asked about this but none of them haved helped me..
I'm using ajax to get some information then I'm trying to pass it to a controller, here is my problem: It seems never get to the controller but I'm not sure what's the problem, also when I try to use the tag's mvc annotation driven it shows me an error, the ajax looks fine. I'm quite sure my problem is the controller or the way I'm calling it or passing the data.
I'll attach some images to show the error it shows up and the files I'm using.
I´m seeing methods of encryption, and I´ve seen a lot of this tool: "aspnet_regiis" but I´ve been asked to search about "machineKey" and all I can see is that it is another section of the web.config but don´t know it´s function or what it is per se...
So... I´m asking this simple question: What is and what does machineKey do ?
Regards.
One of the things that the machineKey does is to validate ViewState so that it can't be manipulated by an attacker. Other uses would be similar. If you are running servers behind a load balancer, you need to make your machineKey the same on all servers so that subsequent requests received by another server can perform validation.
I am trying to make a simple scalable web system, and after looking around a bit it seems that forms authentication with cookies is what im looking for, as I do not need a session for storing login related data.
I have recently plumbed in that part of the system and noticed that the authorise attribute was not doing anything. After a look around the internet it seems that Ninject is causing the issue as it doesn't handle the attribute correctly.
Now if this is the case and it doesn't allow me to use the built in authentication mechanism how am I meant to get the same functionality that authorize would normally give me, without having to write my own authentication system?
As currently it looks like I would have to make my own CustomAuthorizationAttribute and then make a filter with that, which I am happy to do, but I cannot find how to get this to defer to the FormsAuthentication mechanism. Also this only seems to work on a controller level, not at an action level.
I had the similar issue just now (that's how I found your question).
After some googling, I tried moving my IKernel setup code from Global.asax to App_Start\NInjectMVC3.cs, and it miraculously worked.
I'm not sure what caused the initial issue though.
I use Ninject + MVC3 + Authorize attributes with ASP.NET forms authentication using cookies. Everything works as it should.
What do you mean by "the authorize attribute isn't doing anything?" Do you have your membership providers set up in your web.config file?
I'm experimenting with ASP.NET MVC3 Razor and I'm fairly impressed. This is the way we'll go.
We use Spring.NET for dependency injection in our framework and I wonder, if it is possible to use it for MVC3 projects as well?
The documentation of Spring.NET is only talking about MVC2, but I guess there will be MVC3 support in future release. Nonetheless I was trying to get it work though. With no success so far.
So my question is, if someone else found a way or workaround or trick to do dependency injection in MVC3 using Spring.NET and if so, how?
I know this is a very general question, but even a honest "sorry this is definitely not possible" or "it should work without any modifications" would help me a lot.
Thanks in advance,
Jan
Yes, even though the the latest Spring.NET release (1.3.1) has explicit support for MVC2, it can also be (pretty easily) used to support MVC3. The IDependencyResolver interface introduced with MVC3 makes IoC integration significantly more straightforward than it had been in the past (offering just one single interception/pluggability point for type resolution where previously there had been multiple places you needed to intercept type resolution calls).
See blog posts like this one: http://blog.alexkyprianou.com/2011/03/07/using-spring-net-with-mvc-3/ for more information and suggestions on how you might go about doing this (its really quite simple compared to the effort/complexity of doing so with MVC1 and MVC2.
It should work without modifications.
After some more investigation and help of colleages I found out, that Spring.NET works with MVC3 - at least as far as I can see it now. My orignal problem was a misformatted xml-file for my injection objects (I didn't post the code in the question).
Still there seem to be issues with object scopes. The scope request doesn't seem to work, since all my objects are still singletons and once created, can not handle more than one request.
I tried to add the well-known attribute singleton="false" and it worked somehow, so my objects will now be created on every request. Good!
However I found out, that Spring.NET's example Spring.MvcQuickStart.2010 also uses the singleton attribute with value false, so probably this is the correct way (and true by default).
I don't know yet, how to handle the session scope, but at least my web application works for requests (singleton="false") and application-wide (no singleton attribute) with Spring.NET and MVC3.
Best,
Jan
I need to render an ASP.NET MVC view to a string so as to be able to send it via an email (it is an order confirmation email defined in an .ascx file ).
I've successfully been able to render an ASP.NET MVC View to string using one of the methods in this question.
However now I need to be able to do it via a WCF service (that will be accessed via silverlight) and so I don't have a ControllerContext. This WCF service is contained within the same project as my MVC project so has access to all my models etc.
I've looked at several questions on Stackoverflow about this issue, but they all seem to need a controller context. I thought there was something in mvccontrib but it doesn't seem to be there anymore.
The closest I've found is the accepted answer to the aforementioned question, but it unfortunately breaks with RenderPartial within the view you're rendering.
I'm hoping maybe some of the behind the scenes work for ASP.NET MVC 2 related to RenderAction may help make this possible now?
BuildStarted and I have put together a standalone Razor templating engine which you might find useful (Github). You'll need to adapt your .ascx content into a simple string template, but it should do the job.
If you've got NuGet, you can run Install-Package RazorEngine
You may checkout the following blog post. Also Postal is worth looking at.
You need to create a fake HttpContextBase with a fake HttpRequestBase that return meaningful values from their properties.