Thursday, August 24, 2017

Google Cloud Web API c#

1. Create Project  > Google Cloud ASP.NET Core Web API C#
2. Choose Google Project ID (can link with FireBase)

If you want run locally you need to add credential


3. Create  json file,
4. Copy this .json file to locally (outside cannot browse)


public Startup(IHostingEnvironment env)
{
      //Here is the Code for Credential
    string credential_path = "CargoTracking-2e89f93ec38b.json";
    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

------------------------

[GCP Command]
gcloud config list
gcloud auth login
gcloud config set project [myproject]

Wednesday, August 16, 2017

AWS Lambda - Custom Authorization

Lambda Code

Refer

public AuthPolicy Authorization(TokenAuthorizerContext request)
{
    var token = request.AuthorizationToken;

    switch (token.ToLower())
    {
        case "allow":
            return generatePolicy("user", "Allow", request.MethodArn);
        case "deny":
            return generatePolicy("user", "Deny", request.MethodArn);
    }

    return null;
}


At Lambda to Custom Authorization


Add Custom Authorization to API Gateway Method


POSTMAN Test



Monday, August 7, 2017

AWS Lamda

  • Service that lets you run code without managing servers (server-less)
  • Execute your code when needed and scales automatically. (no charge when your code not running)
  • Included Server and OS maintenance, auto scaling,  code monitoring and logging (FOC)
  • Support Node.js, Java, C# and Python.
  • It can run your code in response to events. E.g. Data Processing trigger for Amazon S3 or Amazon DyanamoDB
  • It can build Serverless applications. (only concern your code)
  • Outsider call Lambda thru API Gateway
  • Lambda can be custom authorization  at API Gateway
Step to create Lambda Project in VS2017


1) Need to Install AWS Extension



2) Deployment



3) API Gateway



.NET Core


  • Open Source
  • Cross-platform (Linux, MAC, Windows) framework for building modern cloud-based web applications (UI and API) using .NET
  • ASP.NET Core MVC = MVC + Web API + Web Pages
  • It can run on IIS or Self-hosted


Tuesday, August 1, 2017

Single Sign On (SSO)

ASP.NET Forms Authentication allow to share the same cookie across sites under same domain using configuration key <machineKey>


<machineKey 
  validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D" 
  decryptionKey="ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F"
  validation="SHA1" decryption="AES"/>

Why need <machineKey>, because each ASP.NET web application use it own encryption keys for cookie data and others e.g. ViewState for security purpose. So if the same <machineKey>, across all applications under same domain, each will able to read cookie values.

After that, you need to instruct ASP.NET runtime to define the cookie so it can match with the domain name.

<forms name="name" loginUrl="URL" defaultUrl="URL" domain="mydomain.com"/>

ASP.NET is not able to share cookie across different domain.
_______________________________________________________________________________

Work Around (1)

1)User request domain1.com, no authentication cookie, redirect to domain1.com/login.
2) Login
3) domain1.com accept the login credential, verify from database. and Create authentication cookie  and add to the response
4) Response set ReturnURL to domain2.com.
5) domain2.com accept the response with cookie and store in browser.
6) domain2.com redirect to the  ReturnURL address (domain1.com) with reading the cookie value.
7) domain1.com accept the response with cookie and store in browser.

So both domain1.com and domain2.com authenicaton cookie are stored in the browser.

Problem : You need to implement for all the sites (costly and complex). Not recommend for domain more than 2


Basic_SSO_model_overview.png
_______________________________________________________________________________

Work Around (2)

Browser will not store authentication cookies for each site. It will store an authentication cookie for only in a site (sso.com). Every request to any site will redirect to sso.com for setting and checking authentication cookie. If not found, the user redirected to the login page.

1) User request domain1.com, redirect to sso.com to check cookie, with adding a ReturnURL query string paramater to back domain1.com.
2) If no cookie, request to domain1.com with query string has indicate the cookie not found and redirect to domain1.com/login
3) Login in domain1.com/login and invoke web service of sso.com to check user credentials, and return Token.
4) domain1.com mark the user as logged in, redirect to sso.com with Token to set authentication cookie, with ReturnUrl (domain1.com)

https://www.codeproject.com/Articles/106439/Single-Sign-On-SSO-for-cross-domain-ASP-NET-applic
https://www.codeproject.com/Articles/114484/Single-Sign-On-SSO-for-cross-domain-ASP-NET-appl#_articleTop

Wednesday, April 5, 2017

Enable Cors

JS cannot call Web api, it will get block, add code below to Web API web.config to enable cors.

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <!--<handlers>
     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
   </handlers>-->
  <httpProtocol>
    <customHeaders>
      <!--<add name="Access-Control-Allow-Origin" value="*" />
       <add name="Access-Control-Allow-Headers" value="*" />
       <add name="Access-Control-Allow-Methods" value="*" />-->
      <remove name="X-Powered-By" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

Monday, March 27, 2017

Angular JS 2

Need Node.js, NPM and Git


Module
  • The module component is a block of code which can be used to perform a single task
  • You can export the value of something from the code such as a class
  • The Angular apps are called as modules and build your application using many modules. 
  • The basic building block of Angular 2 is a component class which can be exported from a module.
Component 
  • is a controller class with a template which deals with a view and logic on the page. 
  • Component knows how to render itself and configure dependency injection
  • You can associate CSS styles using component inline styles, style urls and template inline styles to a component.
  • To register component, we use @Component annotation and can be used to break up the application into smaller parts. There will be only one component per DOM element.
Template
  • The component's view can be defined by using the template which tells Angular how to display the component.
  • To display the value, you can put template expression {{name}} within the interpolation braces.
Metadata
  • Metadata is a way of processing the class
  • E.g. we have one component called MyComponent which will be a class until we tell Angular that it's a component. You can use metadata to the class to tell Angular that MyComponent is a component. 
  • The metadata can be attached to TypeScript by using the decorator.
Data Binding
  • Data binding is a process of coordinating application data values by declaring bindings between sources and target HTML elements
  • It combines the template parts with components parts and template HTML is bound with markup to connect both sides. 
  • There are four types of data binding:
    • Interpolation: It displays the component value within the div tags.
    • Property Binding: It passes the property from the parent to property of the child.
    • Event Binding: It fires when you click on the components method name.
    • Two-way Binding: This form binds property and event by using the ngModel directive in a single notation.
Service
  • Services are used for reusable data services to access data (Get Data From API / Databases) to from or share by all components.
  • Angular services are injected using Dependency Injection mechanism. (asynchronous
  • So Component only focus for supporting the view.
  • Service includes the value, function or feature which is required by the application. Generally, service is a class which can perform something specific such as logging service, data service, message service, the configuration of application etc. There is nothing much about service in Angular and there is no ServiceBase class, but still services can be treated as fundamental to Angular application.
Directive
  • The directive is a class that represents the metadata. There are three types of directives:
    • Component Directive: It creates custom controller by using view and controller and used as custom HTML element.
    • Decorator Directive: It decorates the elements using additional behavior.
    • Template Directive: It converts HTML into a reusable template.
Dependency Injection
  • Dependency Injection is a design pattern that passes an object as dependencies in different components across the application. It creates new a instance of class along with its required dependencies.
You must remember the below points while using Dependency Injection:
  • The Dependency Injection is stimulated into the framework and can be used everywhere.
  • The injector mechanism maintains service instance and can be created using a provider.
  • The provider is a way of creating a service.
  • You can register the providers along with injectors.