Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Custom Authorization & Role in MVC 4 not working
1 hour, 52 minutes ago|LINK
 
Hi,
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
 
[Authorize]
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
Posted 28-Feb-13 4:07am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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..[^]
  Permalink  
Comments
kartic_2009 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100