how to authenticate from MVC controller into seperate Web Api project - asp.net-mvc

We are going to build a web project which will potentially use Mobile app as well in the future. So we decide to consume all data works via Web Api. As a start, we will use seperate MVC project. In traditional way, I use Identity2 for authentatication issues and all Controllers of MVC project is secured by [Autherize] attribute. In our scenario, we want to give this job to Web Api project. MVC project will be only consumer of Web Api project. So I am wondering how I can login to Web Api project from MVC project and secure controllers via Web Api. Do you have any suggestions or sample?

Related

MVC Architecture in .Net6

Basically I want to follow MVC Architecture. So should I use Asp.Net Web Api or Asp.Net MVC for backend development?
Does Asp.Net Web Api have MVC architecture? If does help me build Web Api using MVC Architecture in .net 6.
Based on what you've written, you have a couple options.
MVC is a front-end design pattern. For backend development, you could create a Web API project, which would give you the ability to create models and controllers similar to MVC. You would not create views with Web API as there is no UI.
Alternatively, and especially if it's a smaller project where scalability is not a concern, you could create an MVC project representing the whole app and include the backend code there rather than creating a separate backend project. In this case you'd have models and controllers driving the UI, but not the backend.

same domain and one publish for web.api and mvc application?

I have a web api returning json and I have Mvc website consuming that web api. I want to make api calls on the clientside using Jquery, not in the mvc controllers on the serverside. Therefore I don't want to publish them as 2 different application with 2 different domains as it will cause cross site scripting.
ex. mywebsite.com for mvc webapp
mywebsite.com/api for web api
Ok I figured out how to run them under same domain as explained nicely here
But I believe all of these solutions requires deploying the webservice and web api separatly even they run under the same domain?
I want to publish only mvc webapp and api should be published together with it. is it possible?
for example something like "nopcommerce" project is doing with Nop.Web and Nop.Admin? When publishin Nop.web also includes nop.admin and nop.admin is reacheable with the url like nopcommerce.com/admin

How to integrate WebAPI to an MVC application

I am developing an MVC5 application and use Entity Framewerok 6 code first on this. Now we we will also develop an android application that will interact with the MVC application (CRUD operations) by using the web services. At this stage I want to be clarified about the issues below:
1) I think WebAPI is better option for us as we use the services on android apps. What do you suggest?
2) In order to integrate WebAPI to an MVC project, which changes should be made? On the other hand, can we use the same controller and data layer methods (i.e. SaveChanges, etc.) by making some modifications i.e. inheritance? Or do we have to create a seperate methods for web services? Could you give an example by code?
3) Does integrating WebAPI to the MVC project affect the MVC project's abilities or methods? I mean that is there any disadvantage integrating WebAPI to an MVC project?
Any help would be appreciated.
1) That's a good idea. Web API is easy to implement and consume
2) You don't need to make changes to intergate Web API in your application: just start using it. As you want to expose CRUD operations from EF a good idea would be to implement ODATA services. Or use something like Breeze (depending on how you want to consume the services). See "MVC and Web API" bwelow
3) Web API doesn't affect at all the MVC part, unless you make a mistake setting the routes. Although they run in the same host, they work completely independent of each other.
MVC and Web API
Unless you need to do something special, like exposing Web API in a different URL or "domain name", MVC and Web API are implemented in the same web application project. To start using Web API in your MVC project simply add a new controller. Perhaps you'll have to include also the WEB API route configuration, and some other Web API configuration.
If you want to expose the EF model throug Web API you simply have to follow the instructions in the link to create an ODATA controller, which will expose the EF model as a RESTful service, allowing you to execute the CRUD operations to the EF model through URLs.
NOTE: What you want to do is a very frequesnt pattern in MVC applications: MVC is used for generating the views, and Web API fos exposing functionalities that can be easily consumed from the views usin Javascript + AJAX. Don't be afraid to use it. You'll find no problems at all

What could be best MVC project structure with web API?

I am an MVC.NET developer but never used API web service before in an MVC project.
I just want to know like for WCF services we use different project in the application usually. Likewise, what is best practice likewise for web API services and how they work with proxy design patterns extra?
In my current application I am using WCF services but I am keen to learn if API web service can replace it and how?
yes , you can replace with API service.
For your project structure , you can refer below link content.
Implement ASP.Net Web API in ASP.Net MVC 5
http://www.c-sharpcorner.com/UploadFile/4b0136/implement-Asp-Net-web-api-2-in-Asp-Net-mvc-5/

MVC/SPA Authentication Scenarios for Azure AD

These are Application Types and Scenarios that Azure AD supports:
Web Browser to Web Application
Native Application to Web API
Web Application to Web API
Daemon or Server Application to Web API
I have two questions:
I would like to understand where my scenario below fits.
I think I need to use JWT tokens and it seems that Native Application to Web API is the closest,
but I still need Asp.Net MVC application to deliver Client side Angular MVC resources (html templates, controllers and Rest services)
Which Azure Active Directory Code Samples are the closest to my scenario below:
I would like to create a multi-tenant Angularjs (delivered using Asp.Net MVC 5) and Rest Web API 2 secured with Azure AD. I would like to have tenants choose their domain names like firstTenant.com,
smt.firstTenant.com or to have subdomains like firstTenant.MySaaS.com, secondTenant.MySaaS.com
or MySaaS.com/firstTenant, MySaaS.com/secondTenant or similar domain naming scheme.
I would use some kind of IoC container to add customization to my SaaS application or similar to deliver specific functionality to each tenant (GUI and business logic and DB).
I would use and Asp.Net MVC application that will custom tailor SPA resources (html templates, .js controllers, .js services, .css, images etc) to each tenant and use some partitioning techniquest to retrieve tenant and user specific content from DB called from Rest API controllers.
Thanks,
Rad
I am also facing the same 'i dont know' issue :)
But far as i have researched the authorization flow from SPA aplication to the web api.
You still need webserver(mvc) project that will privide redirecting to the Identity provider (azure AD) login page and on the IP callback you will need to inject baerer token to Angular auth service that will send token to the api or deal with the refresh token.
So for me I think that, Web Application to Web API, is the right direction programming.
pls comment if i'm wrong
Currently i'm investigating link
http://code.msdn.microsoft.com/windowsazure/MyCompany-demo-applications-eedab900
update 2:
http://www.cloudidentity.com/blog/2014/04/22/AUTHENTICATION-PROTOCOLS-WEB-UX-AND-WEB-API/
Maybe it will be helpful to us.

Resources