Is it possible to pass logged in user credentials to WCF services? - c#

I currently have a service layer which is exposed via WCF. The service layer includes private endpoints that are to be accessed internally using the TCP protocol but also includes public endpoints (API) for some services to be accessed publicly via HTTP. So far so good... My problem is I don't know how to implement authentication and authorization correctly.
Question 1: If I have an asp.net application that uses asp.net Identity for authentication (Forms Based Authentication) and authorization. Is it possible to pass in those credentials to the WCF service automatically i.e. so that I can perform role based authorization on each service within my service layer? If this is indeed possible can someone provide me with some sample code or point me in the right direction for further reading? If it is not possible? Is there an alternative solution?
Question 2: Do I need two separate databases? i.e. one database for the web application that contains all the users, roles etc and then another database for my service layer that contains all the business data of the application?

Related

What is the recommended way to do WCF username/password authentication in DMZ

I am looking to host a WCF service accessible to a limited set of clients over the internet, as described here:
Best way to secure a WCF service on the internet with few clients
I intend to use Username/Password auth (using existing membership infrastructure) at this stage, with transport security (i.e. SSL).
I'm wondering what the recommended approach would be to processing the authentication request, when the membership database is obviously internal to the corporate network.
Options I'm considering are:
the public firewall would do SSL offload, and then the DMZ server would reverse proxy the request to the internal WCF server - I'm assuming credentials would be retained and passed through with this method, and the internal service could appropriately make use of the Membership provider to authenticate the user.
Write a dumb WCF service to sit externally which will make use of custom username/password authentication. This will call an Authentication Service internal to the network with access to membership details. Once the credentials are validated the call will then basically be passed through to the internal service that implements the required functionality.
Are both of these options possible? Are there any major pros/cons with either of them? (obviously reverse proxy is a lot less code...)
I have decided to use the second option. I already had a membership service internal to the firewall, so I will create the service to sit in the DMZ and implement a custom UsernamePasswordValidator and use it to call the membership service to validate credentials (and down the track I can then implement a custom ServiceAuthorizationManager to check roles/permissions).
Assuming credentials validate appropriately, the service will then call the main service internal to the firewall to carry out the functionality.

WCF Service IIS to build user logged services for external clients

I'm starting in .net and wcf services, sorry if what i'm asking is wrong or has nosense. By the way, sorry for my english too.
I'm trying to build several WCF services hosted on IIS where the internet clients can login with their username / password (info stored in db). When they are logged, they can access their info, see their private documents, change their profile and more actions related to their account.
Well, in asp.net if I get the session after success login, i can build services like "GetMyDocs" and i know "who is", just checking the session username stored with the session ID when he calls the login and his session don't timed-out. But i've noticed that WCF Services are stateless, so this seems i have to send in all requests the username / pwd and check them in the DB before executing the service... always!?
Later, if i want to build a client desktop / Android / iOS application, i will use those services. They then can manage their personal and private data through those apps after a success login.
How should i handle this?
Are WCF Services the way to go or WCF aren't a good choice for that?
How can i handle the user identification with WCF? Because all my services are linked to a user and "GetMyDocs", "SaveNewConfig", "PayItem"... needs to identify who is calling.
Thanks in advance!! Regards!
But i've noticed that WCF Services are stateless
This statement is kind of wrong. Because you could easily create a "WCF-based" web service with state enabled. Furthermore, state is such a broad term that is not worth covering here. For example, you could use session state with a WCF service, or you could set the InstanceContextMode property of a service to one of the following:
PerCall
PerSession
Single
and of course, it all depends on how you configure the service itself.
If your service needs to be consumed by different clients, you should consider at implementing a RESTful service along with OAuth 2.0 or something similar where you can authenticate a request using the Basic Authentication header or by issuing an access token after a successful authentication.
There are a number of ways to provide authentication to a WCF service. One of them is by using a session (basicHttp does not support session, you have to use wshttp, or any of several other transports that support session). However, enabling session is a huge amount of overhead simply to avoid sending credentials on each request.
Session reduces scalability by quite a bit, but if you aren't worried about it... it's an option.

WCF Server-to-server security mechanism

I have a .NET MVC web application with a custom forms authentication implementation that uses a FormsAuthenticationTicket embedded in an HttpCookie to manage session-based security.
We are expanding the system and the .NET MVC server layer is now going to call a secondary layer of WCF services (using HttpClient to call RESTFul services using JSON payloads). The secondary layer of services will be accessible over the internet and must therefore be secured.
What is the best way (and simplest way) for me to secure the second layer of services so that the first server layer can most easily and securely access them. Could I simply embed the existing HttpCookie containing the FormsAuthenticationTicket in the request to the second server layer?
You can secure the channel with ssl and use server authorisation mechanism for it.

ASP.NET Membership Provider and Web Services

I'm working on a web-app using ASP.NET Membership Provieder to implement authentication and roles for each user.
Now I need to create a web service to provide some asynchronous functionalities and I need to call web-methods from several pages in my application. But I've noticed that Web Service is accessible also from non-authenticated users setting the url in the address bar of the browser.
Is there a way to apply Membership Provieder rules also to web-methods, or do I have to implement authentication system for every method?
You should probably not be exposing the web service to the public. It should reside inside your internal network. If you need to expose it to the public then you need to secure it the same way you do with your web application. You could also use Forms Authentication in your web service methods. Depending on the technology you used to implement your service with there might be various ways to achieve that. Basically all your web method calls would require a valid forms authentication cookie to be sent along with each request in order to authenticate the user.

ssl channel wcf web api?

I'm currently developing a web api, with WCF web api, which allows me to write restful apis. One concern I have is security. For this reason I decided to protect my api with the OAuth protocol which works very good. However, the team got to the conclusion that our own applications shouldn't be authorized by oauth, instead they sould be authorized by http basic, meaning that the client application should send username and password.
So I have 2 questions:
How can I set up WCF Web Api to work with SSL, I'm using Web Api preview 6, and the project is a MVC3 application?
I have an operation handler which takes care of the creation of IPrincipal from the client access token, and then injects it into the operation parameters, so I can access the user's info. I would like to have in the same operation handler a condition where I could check if the authorization scheme is OAuth or http basic, and then in the case of http basic extract the user's credentials and authenticate that specific user against my data base, if authentication is successful create an IPrincipal and inject it to the operation parameters. However, as I see it, everytime an application using http basic requests something to the api, I would have to go to the data base and authenticate. So my question is: Am I in the right path, or this could be accomplished in some other way?
Any answers would be appreciate it. Thank you in advanced!
You setup SSL for WCF Web API just like you would any other WCF service exposed over HTTPS. If you are hosted in IIS, then you need to configure a site binding for HTTPS. If you are self hosted, then the configuration is bit more involved. See http://msdn.microsoft.com/en-us/library/ms733768.aspx for all of the details.
To handle basic auth against a custom identity provider you would typically use a custom authz module when hosted in IIS. See [http://custombasicauth.codeplex.com/] for an example of how to do this. If you are self hosted then you can use a custom username passworld validator. See http://msdn.microsoft.com/en-us/library/aa702565.aspx for details.
Yes, I believe you are correct that every request will require authentication unless you establish some sort of session-like semantics.
Hope this helps.

Categories

Resources