MVC 5 IsInRole Usage on Razor Views: Cannot connect to Database - sql-server

I'm having issues using the new identity system in MVC 5, my goal is to make use of the User.IsinRole("RoleName") on Views. For example:
#if(User.IsInRole("Administrator"))
{
<li>#Html.ActionLink("Admin", "Index", "Admin")</li>
}
This is placed in the main layout page which is hit when the application launches. On doing this, i'm getting the following error:
"An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: Unable to connect to SQL Server database."
I have searched high and low for a solution to this, the common solution is to either include "[InitializeSimpleMembership]" at the top of the controller or initialise the database connection manually in application start. (With WebSecurity.InitializeDatabaseConnection). Both of these methods do not seem to be recognised by MVC 5.
I have also tried working around this by creating a bunch of messy code any time i return a view to populate the ViewBag with an IsAdmin boolean by using the Aspnet.Identity.UserManager to determine roles. Whilst this works it's not the way i feel i should be doing things.
It might be worth noting but i don't experience these issues accessing User.IsInRole on the backend, this definitely seems to be an initialization problem.

I was having the same problem, however Stunt's answer wasn't working for me. Tried the answer to this question and that solved the issue.
For the lazy you can try adding this to your web.config file:
<system.webServer>
<modules>
<remove name="RoleManager" />
</modules>
</system.webServer>

I managed to get around the problem by removing the following line from my Web Config:
<roleManager enabled="true" />
This was found when looking after comparing line for line on the following example code:
https://github.com/rustd/AspnetIdentitySample/tree/master/AspnetIdentitySample

Create database on start up of the application. Add the following in the Global.ascx with your dbcontext.
using (FooContext db = new FooContext())
{
db.Database.CreateIfNotExists();
}

Related

GAE not loading properties file

I have created an app-engine app where in I am loading properties files using Spring based configuration and using it in as "${app.id}". When I run my app, all properties are getting loaded properly and my program works like charm but when I deploy the same application in Google App-Engine, I noticed that properties are not getting loaded and am getting null pointer exception. Does anyone has any pointer for this issue. I have googled a lot but not getting any correct solutions. Thanks !!
Use the following in your applicationContext.xml
<context:property-placeholder location="classpath:/test.properties" />

Api calls are blocked by $urlRouterProvider.otherwise('/') if the API is in the same project

Until the end of my Angular application development period, I worked with two seperate projects. WebApi and Angular SPA.
Now I am trying to deploy them to our production server and because of the need for seperate SSL certificates, we decided to combine them and run in the same Project. So far so good...
I managed to combine them together and the server side code compiles. Now when I run the app, the first call to the API results in a parse error with the homepage contents (full of html). I understand that this is due to the $urlRouterProvider 's otherwise attribute, routing all the non-routed paths to the root "/".
I need to fix it, excluding the paths which includes /api in them in order to bypass Angular's routing system out of API's way. Any ideas to help?
EDIT: New project is a combination of WebApi (v2 - Asp.Net) and Client App (Angular v1.3.15 on MVC5 views. And yes, html5Mode(true) but doesn't change a thing if turned off).
EDIT2: Forgot to mention, the error is raised from a jQuery ajax call, outside the angular scope. :$
As interesting it is, I realized some other abnormalities in the way WebApi worked. Did some research and found out that it's because of the latest EntityFramework (v6.1.3). During my merge, I updated EF from v6.1.1 so uninstalled and 6.1.3 and installed back 6.1.1 and the problem is solved! That is very odd. EF 6.1.3 should have been a solid release without any breaking changes. Very odd...
Thanks to all who commented and tried to help.
EDIT: After some time, what I realized is this. EF models should be in sync with the Database in order to make successful database calls from WebApi. When I make an ajax call and the db is not in sync with the models, the problems in the question occures. I hope that information helps someone.

Thinktecture IdentityServer V3 - Data is not binding and resources are not loading

I've recently created an Web API that I'd like secured by Thinktecture's IdentityServer V3. I imported the package from nuget, and was able to hit the token endpoints to create a token. However, I noticed that the bootstrap css would not load on IS's main page:
This is how it appears i.imgur.com/DftMQ7C.png vs https:/demo.getidentityserver.com
It didn't bother me until I started using the views (and not just the endpoints) where my pages would print the variable names and function incorrectly with the resources not loading properly. Like so.
Of course, I can not log in since it redirects me the literal "loginUrl" page. I've tried reverting to the previous version 1.2.1, but I still have the same issue. Alternatively, I've looked at samples and those don't have the same issue at all, so I'm wondering if there's a config in my API project that may be causing this.
Has anyone encountered this problem or know of a potential fix?
Be sure to read through the documentation thoroughly, guys. Lesson learned. I just needed to add RAMMFAR to my web.config "otherwise some of the embedded assets will not be loaded correctly by IIS"
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

OFFSET error with symfony project and MS SQL backend

I am building a symfony 1.4 project with an MS SQL backend. I built a backend using the admin generator but when I attempt to load an admin module I am getting this error:
OFFSET cannot be used in MS SQL without ORDER BY due to emulation reasons
How do I resolve this?
The symfony application is running on Debian squeeze under Apache 2.2.
In my case the error disappeared by setting the pager size explicit:
list:
max_per_page: 20
Edit: That wasn't the solution, I just had some sorting paramters in the session which solved the problem temporarily.
The new solution is to define a defalut sorting. I did it by overriding the index action:
class organisationActions extends autoOrganisationActions
{
public function executeIndex(sfWebRequest $request)
{
// sorting
if ($request->getParameter('sort', 'created_at') && $this->isValidSortColumn($request->getParameter('sort', 'created_at')))
{
$this->setSort(array($request->getParameter('sort', 'created_at'), $request->getParameter('sort_type')));
}
// pager
if ($request->getParameter('page'))
{
$this->setPage($request->getParameter('page'));
}
$this->pager = $this->getPager();
$this->sort = $this->getSort();
}
}
$request->getParameter('sort', 'created_at')
instead of
$request->getParameter('sort')
Based on my glance at the Doctrine API, it is SUPPOSED to support this feature, but may be broken for some versions of MS SQL. Unfortunately I don't have MS SQL any longer to experiment.
If you can get along without doing paging at all, a workaround could be to disable it by setting
list:
max_per_page: false
in your generator.yml file for the modules using admin.
I just ran into this (or a similar) problem. I was getting that error message, and I narrowed it down to a problem with the paging in Symfony. If I took out the paging stuff from the component action, I could get past it.
The answer turned out to be very simple: just order the main query (i.e. the query that the pager gets applied to). In my case, it was adding this:
->orderBy('i.created_at DESC')
Hope this helps someone.
Just an FYI. The backend admin generator pagination get's stuck on certain pages using OFFSET n; at the end of a doctrine query. The solution is to clear the SESSION COOKIES.
I kept getting blank pages in the backend. I analyzed my mysql queries and saw OFFSET 60 was being added to every query. This happened after I had deleted all records from the database table.

ASP.NET 3.5 webforms URL Routing question

I'm attempting to configure URL routing in my ASP.NET 3.5 WebForms app. Setting up routes has always been confusing to me, so I was hoping I could get some direction.
My requirement is fairly simple. I'm setting up different "brands" of my application, and I'd like the brand to be specified in the URL. For example, http://www.mysite.com/brand1/Default.aspx would bring up one brand (code behind would look up brand1 in the db and load specific text, images, themes, etc) and http://www.mysite.com/brand2/Default.aspx would bring up another brand, etc.
I'm fairly flexible with how the URLs are displayed, with the exception of keeping the http://www.mysite.com/brand1 prefix. That cannot be changed.
Thanks for any help!
I do this by using http://www.UrlRewriter.net, as explained here by Scott Gu:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
The trick is to use URL rewriting rules (Regular Expressions) in your web.conifg to get a request for /brand1/default.aspx to actually execute /Processor/Default.aspx?brand=brand1, for example.
NB: The Form.browser trick worked a treat for me when doing postbacks.
Another possible answer from me...
If you're using IIS 7 there is a handy built in module for URL Rewiring, all controlled through a GUI if that's your preference:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Resources