Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Custom Authorization & Role in MVC 4 not working
1 hour, 52 minutes ago|LINK

I need to implement a Single Sign on where a Com+ component should be called to authenticate the user & provide the roles. In short, I need to bypass the default mechanism in MVC 4 where it tries to access the aspnetdb database. So I started with a new MVC4 internet project and added the following code.

In Global.asax
public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
        bool retval = CreateUserObject("John", "pwd");

private bool CreateUserObject(string userName, string password)
        string[] currentUserRoles = { "Admin", "User" };
        GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(userName), currentUserRoles);
        HttpContext.Current.User = userPrincipal;
        //Thread.CurrentPrincipal = userPrincipal;
        return true;

Within the HomeController.cs, I added the [Authorize] attribute for the "About" action as below and it works as expected

public ActionResult About()

However if I modify the [Authorize] attribute to permit only "Admin" role as below I get a runtime error (at the bottom). Is there a way around this to use my own collection of roles for the logged in user, instead of querying the database? I also need to do something similar to the user Profile as well (i.e, instead of database, I should populate the values from the Com+ application.
[Authorize(Roles = "Admin")]
public ActionResult About()

Server Error in '/' Application.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)M

1 solution

First and foremost make sure that the SQL Server services are running. Other reasons could be, incorrect connection string or some Firewall blockage.

Here, have a look at this blog post to troubleshoot the issue : MSDN Blogs: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified[^]

If needed, look at these one:
Exception - error 26 - Error Locating Server Instance Specified[^]
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance...[^]
"Specified SQL server not found" o..[^]
Share this answer
kartic_2009 28-Feb-13 12:02pm    
As I mentioned, I want to prevent the MVC application from using the SQL Database connection. Instead I want the ability to authenticate and authorize the user by querying a Mainframe repository via an existing Com+ component.

We have a unique requirement, because all customer information is stored in a Mainframe system, and we already have a legacy ActiveX component that queries it and gets the products that the user has purchased etc (i.e as part of the user's profile). Users are created and deleted in the mainframe system, so there's no point in using the default SQL Server ASPNETDB mechanism as it'll never be kept upto date.

In the existing setup we have a bunch of Classic ASP applications sitting across multiple servers. They all call this common ActiveX component which creates the Authentication cookie & also supplies the user's profile information by querying the Mainframe repository. It also exposes methods to validate the cookie etc. This enables single sign-on for all the websites since the same ActiveX component authenticates all the websites. Now I've to find a way to make the MVC application to co-exist with the remaining apps by using the same mechanism, so that the user can login once and seamlessly move from one application to another without having to login again.
Sandeep Mewara 28-Feb-13 12:24pm    
If you don't want to use SQL, then why are you trying to troubleshoot/share the error and solve for it? Keep question simple and just what you are looking for.
Sandeep Mewara 28-Feb-13 12:25pm    
BTW, strange requirement of authentication via COM+ component. Sorry, I don't have much idea about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900