Breeze js with aspnet identity?` - breeze

Has anyone managed to get Breeze js working with the aspnet identity webapi 2? The only example I've seen with ajax and webapi 2 is the durandal 451 project which uses jquery ajax calls and a separate controller: https://github.com/Useful-Software-Solutions-Ltd/Durandal451/blob/master/Durandal451v2/Controllers/AccountController.cs
Would it just be a case of copying all these methods into the breezecontroller?

NO. Don't do that. Don't combine auth concerns with data access concerns in the same controller.
A Breeze Web API controller is ... a Web API Controller. Use the same techniques that are appropriate for any Web API controller. I suggest you start down this road by reading the Web API Security documentation. Everything you learn there ... and thereafter ... will apply.

I dont know what you're using for spa framework but I managed to run angular, breeze, and webapi2 auth at the same project. I cant show you a working example for now, it's a private project. if I got time, gonna put a super simple example to github.
Here is how I did it;
Started using this project as a boiler plate for angular and oauth web api,
http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/
and John Papa's hottowel mixing after that for again angular and breezejs.
What you need to understand is breeze controller is just a controller and has nothing to do with auth as Ward mentioned below.
hope this helps.

I can vouch for osmancelik's answer. I did the exact same thing. I used the HotTowel template and implemented the code from http://bitoftech.net/2014/06/09/angularjs-token-authentication-using-asp-net-web-api-2-owin-asp-net-identity/ together with breeze. Works like a charm.
I ran into one problem though. The token authentication code adds a service to httpProvider interceptors.
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('authInterceptorService');
});
This works perfectly for default angular ajax calls but not for breeze (when you start from the HotTowel template) Adding the breeze.angular.js service was all that I needed to do to fix this.

Related

Linking MVC controllers and Actions with Angular 2?

I really stucked in MVC + Angular 2 project in the first Step. The problem is, How to connect controllers and actions with Angular 2? Is there any routing needed?
I have done with all setups with Visual Studio 2015 to work with Angular 2. And it successfully works. But i really doing my projects in ASP.NET MVC 5 (Not ASP.NET Core). Here i dont know how the Controllers and Actions work with Angular. How can i route (navigate) to several controllers and actions in my project.
Since MVC consists of several controllers and actions. There will be many views for several actions and for every action there will be a GET(View) and POST(Form Submit) method also.
If anyone work with Angular 2 + ASP.NET MVC let me know how the Controllers and Actions are getting connected.
It looks like you are building your first SPA that communicates with a REST API.
For starters, you should get two things straight:
The ASP.NET MVC is your REST API. It will expose endpoints that can be requested via HTTP calls.
Your Angular 2 is the SPA (Single Page Application), which will make HTTP calls to the REST API you wrote in ASP.NET, and that API will give back responses of whatever you want. You can then use that response data to your liking in your Angular 2 frontend.
That being said, the first step you want to do is make sure you can properly access your API endpoints. Download a tool called Postman and test this with ease.
Ideally, you want to make this API a JSON API, because that will be the easiest thing to work with for your frontend. Plus, it's basically the de facto standard. But as always, use what works best for your use case.
Once your endpoints are working as intended, what you need to do is make some AJAX calls in your frontend that will hit those server endpoints. For Angular 2, you can use the Http package.
You will undoubtedly come across a CORS (Cross Origin Request Sharing) browser error when making the calls to your server. This is basically the browser refusing to send requests to your server until your server explicitly says you are allowed to make requests to it, assuming you are not on the same host and port.
The above was a really brief and limited-detail breakdown of what you need to do. For an example of putting it all together, check out my Angular 2/Golang chat room app. As you will see from this file, I expose some endpoints to allow create/read/update/delete actions for Todo's in my Go server. And the Angular 2 code is in this directory.
One last tip: If you are serving your front end files and exposing your API endpoints all on the same ASP.NET server, as opposed to having one ASP.NET server for the REST API, and one for the serving of UI files, then two things are true:
You need to have a catchall route in the server that will serve your index.html. You can see an example of this in this file. You'll notice a long that starts with r.NoRoute...that is where I say "no route was found so far, therefore serve index.html and let the Angular 2 handle the routing from there".
You no longer need to worry about CORS errors
Hope that helps!
Not actually, you will create and Expose APIs (WEB API) in your MVC project and your Angular 2 will consume those APIs.

Angular JS with MVC

I want to know if Angular JS can be used with ASP.NET MVC architecture in any way? I have tried searching on Google, but can't figure out if Angular JS is usable in .cshtml page of ASP.NET MVC and is it the good idea to use it?
you can definitely use AngularJs with Asp.Net MVC as it is a client side framework which entirely runs in the browser. It doesn't depend on any of the Asp.Net features as such. There are lot of samples available online. Here is a channel 9 video which guides you through
From the official ASP.net pages: Hands On Lab: Build a Single Page Application (SPA) with ASP.NET Web API and Angular.js
The fact that is uses WebAPI shouldn't really matter here, as that's a detail matter of implementation. AngularJS works fine on top of either.
First hit on google...
If you looking to achieve CRUD in your application I recommend this post as a guide to follow. Even though this uses WEB API, its still a good way of getting to grips with angularjs.
Hope this helps.
CRUD with SPA, ASP.NET Web API and Angular.js

Implement Basic Auth in ASP.NET 5

I’m using ASP.NET Identity with Forms auth in an MVC6 application, and trying to implement Basic auth for the API endpoints.
I would have thought there’d be a pre-built middleware that does it, but haven’t been able to find it. I’ve had a look at the Filters test website https://github.com/aspnet/Mvc/tree/dev/test/WebSites/FiltersWebSite and I can’t seem to work out what it’s doing/which parts are important.
I’ve tried using the pre-5 approach of manually doing basic auth in an authorizationfilter, but SuppressFormsAuthenticationRedirect seems to be gone, and the CookieAuthenticationHandler keeps redirecting to the login page.
Basic auth is not there by design. Security team is against it. They will probably make a sample later.
you can read a discussion on the issue here : https://github.com/aspnet/Security/issues/209
Since you are targeting IIS, I suggest leveraging IIS for basic authentication. You can drop a web.config file into your wwwroot folder with a section to configure your basic authentication options.
If you want to remain host agnostic, you will need middleware. Here is someones implementation: https://github.com/Kukkimonsuta/Odachi/tree/master/src/Odachi.Security.BasicAuthentication

ASP Web API method access from regular MVC controllers

I have a rather complex solution which I implemented using ASP Web API, among others. I have 3 projects in my solution worth mentinoing right now. One is a WebAPI containing only api methods. Other two are are the backend and frontend for my application. The backend uses a SPA approach loading mostly empty shells for views and filling everything with ajax acessing the API. The frontend on the other hand because of SEO concerns was decided to be implemented by more traditional means, aka most stuff is rendered server side. My question is, is it possible and good practice to simply call the web api methods from the frontend controlllers and send the results to the view? I don't see a point in duplicating most code in the regular controllers since it's all done with the api.
Any samples on this? I've been searching but couldn't find much.
If you need to call Web API service from C# code (MVC controllers or elsewhere), HttpClient or WebClient can be used to call the services over HTTP.
If you need to simply reuse code, it should be abstracted into a class library (DLL) and referenced from the Web API and MVC projects.
I've run into this same situation and have used the Web API controllers from MVC controllers for a little while at least. You can do this simply by creating new objects of the Web API controllers then calling the appropriate methods off of them. I found this method works fine initially but creates the dependency that means your Web API can't change without also changing the MVC controllers as well.
My advice is to put as much functionality on your models that makes sense with partial classes, and if that is still inadequate then create another logic tier that contains all the shared business logic. You should not have duplicated logic in your MVC and Web API controllers, they should just serve as the glue to get the data served.

accessing wcf from mvc view trough js

I'm trying to learn wcf on practical example. I followed scalable wcf solution tutorial and my service works ok, client from my console app. works ok. But what I want to achive is consuming my service trough js from mvc view (razor) page. On my client console application I'm accessing to proxy with
IService proxy = new ChannelFactory<IService>(Configuration.MyServiceActiveEndpoint).CreateChannel();
List<MyObjectDto> data = proxy.GetMyData();
...
how to practicaly achive this creating proxy client from mvc view page (without adding service reference). Thank you
You don't do this.. you really really don't. This completely breaks the entire point of Model-View-Controller (MVC). The controller should be the one accessing the WCF service and returning the data to the View.
Check out servicestack.net for the cleanest and best web service implementation in .net. No config, easily callable from jQuery, and returns json by default. Easy to get started with NuGet Mvc 3 package.
The tutorial you are using already registers an endpoint with the enableWebScript behavior - you should get a js proxy generated automatically when you access http://server/virtualdirectory/X.svc/json url. Include that js file in your mvc view.
If you host the wcf service in the MVC web app you can use Url.Content:
<script src="#Url.Content("~/X.svc/json")" type="text/javascript"></script>
Then just invoke the service from js - use the javascript from this post as an example: http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx

Resources