How to consume web API with authentication in Excel 2010 - asp.net-mvc

I have an exmaple of mvc 4 web API , and I want to consume it in Excel 2010. whats the easiest way to do it? I am thinking to pass in the username and password as string parameter if solution need to be complicated.
[Authorize]
public class AccountBalanceApiController : ApiController
{
//http://localhost/FTAccounting/api/AccountBalanceApi?companyId=16&fiscalYear=2012
public Dictionary<int, TrialBalanceAccountSummaryModel> Get(int companyId, int fiscalYear)
{
return AccountBalance.GetTrialBalance(companyId, fiscalYear);
}
}
Update: Web service Authentication is not possible in Excel. Reference from http://cwebbbi.wordpress.com/2012/07/31/using-the-webservice-function-in-excel-2013/

web service Authentication is not possible in Excel. reference from http://cwebbbi.wordpress.com/2012/07/31/using-the-webservice-function-in-excel-2013/

In modern Excel you can use Get & Transform to connect to Web API with authorisation. Select Data tab, then in the Get & Transform group New Query -> From Other Sources -> From Web (Note: do not confuse this with 'From Web' button which is directly in the Get External Data group). Enter your endpoint URL (you can also define request headers here) and in the next step you will be asked for authentication method.

Related

How can I design better communicate between application members and Web API and Signalr Hubs

I am working in a project which needs to communicate with users realtime. Basically I am following "synchronize pages over web api" path which is introducing in this video (also Brad Wilson has a nice video like this one) and the repository here for video.
My question is about mapping application members with their ConnectionIds which is producing by Signalr.
I used to be saving every signalr connection ID in a database table like:
ConnectionID
MemberID
ConnectionStatusID --> Connected(1), Disconnected(2), Reconnecting(3)
I was fetching member's connection IDs in most requests.
Then I decided to change this design. Now I am grouping every member with a unique string when OnConnected and OnDisconnected like this:
public override Task OnConnected()
{
Groups.Add(this.Context.ConnectionId, string.Format("MyHub_{0}",HubUser.MemberID));
return base.OnConnected();
}
public override Task OnDisconnected()
{
Groups.Remove(this.Context.ConnectionId, string.Format("MyHub_{0}",HubUser.MemberID));
return base.OnConnected();
}
Now I am not fetching anything from database about member connection IDs. It is only working over Web API for making synchronization between browser tabs.
I simply show how I am handling incoming Request and using Hub in Web API:
public HttpResponseMessage Example()
{
string msg = "Example to all";
//some other process
//Hub is an instance of a IHubContext
Hub.Clients.Group(string.Format("MyHub_{0}", HubUser.MemberID)).example(msg);
return Request.CreateResponse(HttpStatusCode.OK, msg);
}
So which one do you think is efficient way for using Signalr. What would you suggest is possibly better than these. Is making a dependency between hub and WebApi is a good design choice to go.
And also project will be deployed to multiple servers and Load Balancer will work in front of them (I am going to use Sql Backplane for this). There are few projects which must talk to Web API, indirectly to hub too.
I like your second solution as it should require less DB queries. It is no less efficient for SignalR to send to a group than it is for it to send to a connection id. You just need to ensure your group names (HubUser.MemberID) are unique and non-spoofable.
In SignalR 2.0, we make this pattern even easier: http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections#IUserIdProvider
If your users' MemberIDs match up with their IPrincipal.Identity.Name, then you can just change all your code to use Clients.User(HubUser.MemberID)....
If you don't want to base your SignalR user id on the request's IPrincipal, you can provide your own IUserIdProvider and inject it using SignalR's dependency resolver.
http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection

Is it normal to create RESTful based web simple online store?

I design a simple online store. It has a product card, a list of products, the ability to add comments to the product, user registration, search products by price and other criteria. Maybe online store will have mobile clients (android and ios).
I want to try to work with RESTful. Is it normal to create this store, using RESTful? If it is normal, then I have a few questions.
When I create the usual sites, I write the following code:
public ActionResult Index()
{
var products = this.productRepository.GetAll();
return View(products);
}
How to change the architecture of the site, if I use RESTful. What must this method sent to client? Html only? And then the client has to execute AJAX request to the api to get the data?
RESTful web services is an HTTP-based services, any HTTP related applications can implement it by using WebApi,WCF, etc.
To build the online store project, of course you can use WebApi to build RESTful web services.
It's hard to say if using RESTful web services is normal or not, you can build an web application without building RESTful web services.
It only depends on your needs and preferences.
Let's say you're using WebApi to build RESTful web services for your project.
below is an example showing how you can implement it.
WebApi Controller
public class ProductsController : ApiController
{
public IEnumerable<Product> GetAllProducts()
{
return this.productRepository.GetAll();
}
}
MVC Action
Example 1: Calling Web API controller actions from MVC action
public ActionResult Index()
{
var webApi = new ProductsController();
return View(webApi.GetAllProducts());
}
Example 2: Populate all products in the view by calling Web API using Ajax
View:
<div id="contents"></div>
JS file
$(function() {
showAllProducts();
function showAllProducts() {
var url = "http://localhost:13131/api/Products/";
$.getJSON(url, function(result) {
$("#contents").append(result); // here you need do more than this.
});
}
});
What way to implement Web API depends on your need, there are a lot of way to implement it.
For your project, it's good to try different methods, you can learn more about it in the process.
Hope it helps.

Breeze.WebApi2 Authentication Microsoft.AspNet.Identity

Now that AS.NET WebApi2 has an integrated authorization mechanism using tokens.
How can we integrate this mechanism with Breeze.WebApi2. That is Breeze.WebApi2 and Microsoft.AspNet.Identity.
What I need is how to modify the project created using visual studio 2013 project wizard that is configured to use individual accounts in security into a breeze web api 2 server with the same security setup.
I tried it by creating a web api project with individual accounts security and then added breeze web api 2 server using nuget package but that was s bit confusing to me.
A sample code or application would be more useful for me to get started.
Thanks.
If you use the Authorize attribute you can also access the user data via the "User" variable.
[BreezeController]
[Authorize]
public class NorthwindIBModelController : ApiController {
[HttpGet]
public IQueryable<Customer> CustomerList() {
var userName = User.Identity.Name;
var filter = filter on customers;
var custs = ContextProvider.Context.Customers.Where({ some filter using userName});
}
}

OData - Data Service Simple Authentication

I would like to add simple authentication to Data Services, for now only to restrict access for particular applications by simple token.
I don't need Domain Authentication or Forms authentication.
I read a lot about authentication here:
http://franssenden.wordpress.com/2010/06/14/custom-security-odata-service-wcf-data-services/
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/06/03/10482.aspx
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/01/15/10119.aspx
http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/01/10/10100.aspx
Unfortunately it all demands a loooot of work.
Most of all creating custom IHttpModule.
There should be more simple solution.
I know that when I create object context on the client (WPF) I can add Credentials.
Uri uri = new Uri("http://localhost/myapp/odata.svc");
MyEntities ent= new MyEntities (uri);
ent.Credentials = new NetworkCredential("token", "zx5as9vxc5sa9h0vb6523cv56");
But where can I read them (without implementation of custom IHttpModule)?
I thought that I can use something in class that is implementation of Data Service for example:
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
string cred = args.OperationContext.AbsoluteRequestUri.UserInfo;
}
I'm not familiar with UserInfo but description for it stands "Gets the user name, password, ...)
So I have two main questions:
Where can I read Credentials included by typing ent.Credentials = new NetworkCredential("token", "zx5as9vxc5sa9h0vb6523cv56");
Where can I (if I can) set UserInfo on the client app and use it in OnStartProcessingRequest method.
Regards,
Daniel Skowroński
There's a series of post about authentication and WCF Data Services (which is the .NET implementation of the OData protocol): http://blogs.msdn.com/b/astoriateam/archive/tags/authentication/
You should be able to find lot more information there (including code samples).

log traffic (including posted data) IIS 6.0 - windows server 2003

I understand that IIS logs parts of http request which I can access. I would like to log the whole http request for a short period of time. This means I would like to store the data being posted in its raw form. Is this possible using ISS’s logging facility or do I have to install another tool?
I have the following problem. I expose a ‘restful web service’ via asp.net mvc which stores posted data in a relational database. I tested the service via javascript. I url encoded some data and post them using jquery. The data is stored url encoded in the database as expected. Some clients also post data using unix and wget to the same web service. Unfortunately, the data is not stored url encoded (so we lose some data if it contains special characters like &). They claim that they send the data url encoded. Can this be a case true? Is the mechanics of wget post different to that of a javascript post? Is there a layer I overlook? I would like to double check whether the data send via wget is actually url encoded.
Thanks!
Best wishes,
Christian
You could create an MVC action filter, you could then have access to the Request object and pull out the interesting bits. You could then store this information in a text file or logging database. I've used this approach before and it works well if you thread the saving to database operation to not slow down the general request operation.
public class LogRequest : ActionFilterAttribute, IActionFilter
{
#region IActionFilter Members
void IActionFilter.OnActionExecuting(ActionExecutingContext var)
{
//code goes here
}
#endregion
}
[AcceptVerbs(HttpVerbs.Get)]
[LogRequest()]
public ActionResult Index()
{
return View();
}
You've kind of asked two questions here, I don't know about the second part.

Resources