Returning binary file from controller in ASP.NET Web API
Source Try using a simple HttpResponseMessage with its Content property set to a StreamContent:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// using System.IO; // using System.Net.Http; // using System.Net.Http.Headers; public HttpResponseMessage Post(string version, string environment, string filetype) { var path = @"C:\Temp\test.exe"; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(path, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return result; } |
A few things to note about the stream used: You must not call stream.Dispose(), since Web API still needs to be able to access it when it processes the controller method’s result to send data back to the client. Therefore, do not use a using (var stream = …) block. Web API will dispose the stream for you. Make sure that the stream has its current position set to 0 (i.e. the beginning of the stream’s data)….
ASP.NET MVC – Set custom IIdentity or IPrincipal
Source I decided to use IPrincipal instead of IIdentity because it means I don’t have to implement both IIdentity and IPrincipal. Create the interface
1 2 3 4 5 6 |
interface ICustomPrincipal : IPrincipal { int Id { get; set; } string FirstName { get; set; } string LastName { get; set; } } |
CustomPrincipal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class CustomPrincipal : ICustomPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public CustomPrincipal(string email) { this.Identity = new GenericIdentity(email); } public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } |
CustomPrincipalSerializeModel – for serializing custom information into userdata field in FormsAuthenticationTicket object.
1 2 3 4 5 6 |
public class CustomPrincipalSerializeModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } |
LogIn method – setting up a cookie with custom information
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
if (Membership.ValidateUser(viewModel.Email, viewModel.Password)) { var user = userRepository.Users.Where(u => u.Email == viewModel.Email).First(); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.Id = user.Id; serializeModel.FirstName = user.FirstName; serializeModel.LastName = user.LastName; JavaScriptSerializer serializer = new JavaScriptSerializer(); string userData = serializer.Serialize(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, viewModel.Email, DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); Response.Cookies.Add(faCookie); return RedirectToAction("Index", "Home"); } |
Global.asax.cs – Reading cookie and replacing HttpContext.User object, this is done by overriding PostAuthenticateRequest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); JavaScriptSerializer serializer = new JavaScriptSerializer(); CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData); CustomPrincipal newUser = new CustomPrincipal(authTicket.Name); newUser.Id = serializeModel.Id; newUser.FirstName = serializeModel.FirstName; newUser.LastName = serializeModel.LastName; HttpContext.Current.User = newUser; } } |
Access in Razor views
1 2 3 |
@((User as CustomPrincipal).Id) @((User as CustomPrincipal).FirstName) @((User as CustomPrincipal).LastName) |
and in code:…
WEB API 2 EXPLORING PARAMETER BINDING
Source This article demonstrates how to call or map Web API methods with different types of parameters using XML, Json and Urlencoded formats. It shows how simple parameters, objects parameters and array parameters can be sent either in the body of the Http request or in the Url itself. This all works per default in Web API and if that’s not enough, you can customize it yourself. code: https://github.com/damienbod/WebApiParameters Simple Parameters Example 1: Sending a simple parameter in the Url 1 2…
Send string array to web api
You can send it in the body or the URL URL:
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="com">// http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9</span> <span class="pun">[</span><span class="typ">Route</span><span class="pun">(</span><span class="str">"example6"</span><span class="pun">)]</span> <span class="pun">[</span><span class="typ">HttpGet</span><span class="pun">]</span> <span class="kwd">public</span> <span class="kwd">string</span> <span class="typ">GetListFromUri</span><span class="pun">([</span><span class="typ">FromUri</span><span class="pun">]</span> <span class="typ">List</span><span class="str"><int></span><span class="pln"> paramsObject</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">paramsObject </span><span class="pun">!=</span> <span class="kwd">null</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="str">"recieved a list with length:"</span> <span class="pun">+</span><span class="pln"> paramsObject</span><span class="pun">.</span><span class="typ">Count</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="str">"NOTHING RECIEVED..."</span><span class="pun">;</span> <span class="pun">}</span> |
or in the body:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span class="pun">[</span><span class="typ">Route</span><span class="pun">(</span><span class="str">"example8"</span><span class="pun">)]</span> <span class="pun">[</span><span class="typ">HttpPost</span><span class="pun">]</span> <span class="kwd">public</span> <span class="kwd">string</span> <span class="typ">GetListFromBody</span><span class="pun">([</span><span class="typ">FromBody</span><span class="pun">]</span> <span class="typ">List</span><span class="pun"><</span><span class="typ">ParamsObject</span><span class="pun">></span><span class="pln"> paramsList</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">paramsList </span><span class="pun">!=</span> <span class="kwd">null</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="str">"recieved a list with length:"</span> <span class="pun">+</span><span class="pln"> paramsList</span><span class="pun">.</span><span class="typ">Count</span><span class="pun">;</span> <span class="pun">}</span> <span class="kwd">return</span> <span class="str">"NOTHING RECIEVED..."</span><span class="pun">;</span> <span class="pun">}</span> <span class="typ">User</span><span class="pun">-</span><span class="typ">Agent</span><span class="pun">:</span> <span class="typ">Fiddler</span> <span class="typ">Content</span><span class="pun">-</span><span class="typ">Type</span><span class="pun">:</span><span class="pln"> application</span><span class="pun">/</span><span class="pln">json </span><span class="typ">Host</span><span class="pun">:</span><span class="pln"> localhost</span><span class="pun">:</span><span class="lit">49407</span> <span class="typ">Content</span><span class="pun">-</span><span class="typ">Length</span><span class="pun">:</span> <span class="lit">91</span> <span class="pun">[{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">3</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">76</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">19</span><span class="pun">},{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">56</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">87</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">94</span><span class="pun">},{</span><span class="str">"Id1"</span><span class="pun">:</span><span class="lit">976</span><span class="pun">,</span><span class="str">"Id2"</span><span class="pun">:</span><span class="lit">345</span><span class="pun">,</span><span class="str">"Id3"</span><span class="pun">:</span><span class="lit">7554</span><span class="pun">}]</span> |
Here’s example of parameters bindings: http://damienbod.wordpress.com/2014/08/22/web-api-2-exploring-parameter-binding/
Use the On-Premises Organizational Authentication Option (ADFS) With ASP.NET in Visual Studio 2013
Source This afternoon my good friend Pranav Rastogi pointed out that we don’t have a walkthrough showing how to use the On-Premises option for organizational authentication in the new ASP.NET project templates in VS2013 – AKA hooking up your web app to an ADFS instance. You know what? He was right! With all the excitement around the new capabilities for cloud-based project, we didn’t cover this specific feature as that is largely a refactoring of what was already available in the Identity and…