Web API and Entity Framework user name issue - asp.net-mvc

All,
I have a ASP.NET Web API project that is making a REST call to my service layer in another project. The service project's data access is via Entity Framework 4.3. The connection string in the web.config files is set to use Integrated Security.
What is happening is, the name of the server, "server A", is being passed to the service layer, and failing authentication against SQL Server. There isn't a user account named "server A."
More specifically this is what the architecture looks like
jquery file making an api controller via POST to a method within the API controller
API controller method references the service layer DLL, and calls a method within the service class
The service class is calling a method in a repository class that uses DbContext to connect to SQl Server 2008 table.
Is there something specific I need to be doing when using the Web API framework in order to pass the correct user name down to EF?
Any help would be appreciated.
Derek

The problem is double hop impersonation. You can read about it by this link.
But i'm not sure that such impersonation is possible via REST. I recommend you use database via special account, not integrated security.

Related

How to create Two Applications in Dotnet Domain using (MVC ,WebServices (or) Class Library)?

How can I create Two Applications in Dotnet Domain using (MVC ,WebServices (or) Class Library)?
I want to create Two application
1)which is having Database Connection(Developed One)
2)Client Application - With out Database Connection Need to get the
data from Application(1).
May I know which technologies is usefull?
A) MVC(Through Ajax Call will Connect) - WebServices(Having Database
Connection)
B) MVC(Through Ajax Call Will Connect) -Class Library(BY Sending
Class dll).
So, I need combination of technologies .
In Client Application I don't want a Database connectionstring.
Please help me.
Thanks in Advance
you need a Web Api not an mvc application to interact with db and and client application choice is upto you that can be anything mvc scf spf android
now the main thing is if you use Web Api then you can create multiple client application over multiple platforms without worrying about the code structure or platform since the webapi can return result in json format
so go for WebAPI for your application

Why is the company file name supplied by the web service?

I understand that to access a company that is not currently open in QuickBooks, the web service needs to supply QuickBooks Web Connector with the file location as a return value to an authenticate() call.
This seems backwards to me. Why would the web service be in charge of telling the Web Connector where the relevant company file is? Wouldn't it make more sense for it to be managed by the Web Connector?
Here's the relevant explanation I've found within the QuickBooks Web Connector
Programmer’s Guide:
IF your web service wants to try a different company, supply the company pathname in the returned string. (You can supply an empty string if you want to use whatever company file happens to be open.) The web connector will respond by attempting to connect to QuickBooks again using that supplied string.
Why Would a Web Service Try a Different Company?
Why would a web service perform the second of these actions instead of simply just stopping altogether? In practice this approach is used when the web service remembers the company file path from session to session (a recommended practice) and wants to have a fall-back to use whatever company file is currently open in QuickBooks (by responding to the connectionError call with an empty string).
This is not as haphazard as it might seem. When a web service is added to the web connector, the web connector stores a unique FileID as a private data extension in the specified company. As a result, the web service can always verify that it is talking to the expected company file simply by checking the CompanyRet returned to your web service in the web connector’s first sendRequestXML call in the data exchange sequence. (Check the data extension list for the expected FileID.)
This seems like a poor end-user experience; if they move their company file (assuming they want the Web Connector operate without QuickBooks open), the web service will fail until that path is updated on the server side. It seems totally plausible that an end-user could do this without knowing it would break things.
Why is it structured this way? And more importantly: is there a way around this?
Why is it structured this way?
Because this is how Intuit built it.
is there a way around this?
No.

ASP.Net Identity using a custom auth provider/service

I am currently developing an ASP.Net MVC web application that requires username and password authentication. I started looking into using ASP.Net Identity for this however I have a very important requirement, the requirement is that the web application itself has no direct access to any databases, all DB Access is to be exposed to the application via an internal REST service. This is due to certain security polices we follow.
I realise that ASP.Net identity is capable of supporting external authentication methods but my question is split into 2 parts.
1) How would I configure ASP.Net Identity to use my custom REST service for authentication?
2) How would I go about developing a service that can be used by Identity for authentication ? (what would need to be returned from the service to ASP.Net Identity)
Any help on this would be most appreciated.
I just did what you are asking about. First, as FPar suggested, you need to implement an IUserStore and pass that to your UserManager. Your custom IUserStore will implement the interface, I used Resharper to generate stubs, but instead of using entity framework, you will use HttpClient to make calls to your REST service.
The REST service will have one action on a controller, I called my identityController, for each of the interface methods you actually need. I implemented the userstore, userloginstore and the rolestore, with code for about 10 calls I actually used. The identitycontroller then is what actually accesses the database.
I also retained the fully async pattern, using async REST calls and Database looks, both with and without entity framework. A shortened version of my data access code is in another question here, regarding IUserLoginStore::AddLoginAsync. In that class I actually used the original entityframework implementation of the user store for part of work, and eventually settled on plain (except for async) ado.net for the parts I couldn't make work that way. The tables are simple enough, using your ORM of choice would not take a lot of time.
Good luck!
You want to implement your own IUserStore and then pass a reference to the UserManager. Look into the Startup and the IdentityConfig files in the standarad ASP.NET MVC with individual user account authentication, to see, how to use them.
You can look here for an IUserStore implementation with entity framework. This is a template, you could start from and change it to your needs. However, you don't have to implement all interfaces, just implement the interfaces, you really need. The UserManager is able to handle that (it throws an exception, if you call a method, that requires an interface, that you don't implement.)
These are two excellent articles on this subject:
http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server
http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics

Is it possible to get SimpleMembership to call a web service?

My web database server is being moved from the DMZ to the company LAN (don't ask why!). The downside of this is that my MVC 4 website is now gonna have to talk to an app server with web services (or web api) to get to the database. This isn't a major headache for most of my stuff, but I'm using the SimpleMembershipProvider for user authentication and that seems to need direct access to the database.
Is there any way to get SimpleMembership to talk to a web service or do I have to use a custom membership provider instead ?
I'm faced with the same situation, and while I haven't written or tested the code yet, I'll be writing a class to inherit from SimpleMembershipProvider and overriding the methods to call methods from the service (web api).
Is this the approach you ended up using?

How to do ServiceStack auth with MVC without EF

I'm working through the example AccountController and trying to implement my own. I'm trying to do OAuth (via GMail) and so I've got it working all the way through ExternalLoginCallback(string). The example does OAuthWebSecurity.Login. When I try to also do that, I get:
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class.
After digging in, it appears that InitializeDatabaseConnection uses Entity Framework to configure a database on SQL Server. I'm not using SQL Server (Mongo DB), so I can't use this method. How do I use ServiceStack auth with MVC and no SQL Server?

Resources