Tuesday 26 September 2017

Response to Preflight Request Doesn't Pass Access Control Check: The 'Access-Control-Allow-Origin'

In the tip, I would try to explain scenario where a developer might come across a situation trying to login using Angular http POST data typically email, password some Token with Web Api. If both the client , server app are hosted in the same domain, then there won't be any issue, but this is something that is not same always.<11>
Let's Say client running on http://localhost:4200 and API is running on http://localhost:50000.In this case when client tries to access the api via  http POST method, then the application is likely to throw exception somewhat like below:

"Fix To Response to preflight request doesn't pass access control check: 
<11>
The 'Access-Control-Allow-Origin' header contains multiple values"

There are several ways to resolve this issue:

1. Adding the following customHeaders to the web.config of the Web API server.

<httpprotocol>    <customheaders>        <add name="Access-Control-Allow-Origin" value="*" />        <add name="Access-Control-Allow-Headers" value="Content-Type" />        <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />        <add name="Access-Control-Allow-Credentials" value="true" />    </customheaders></httpprotocol>


After adding above keys.The issue is gone.
2. Remove the headers from the web.config mentioned above.
3. Search for CORS in the nuget package manager in visual studio.
4. Apply the below attribute on controller level:
 [EnableCors(origins: "*", headers: "*", methods: "*")]
5. In order to apply CORS to all controllers in Web API, one can include the following as the first line in Register method in WebApiConfig.cs.

public static void Register(HttpConfiguration config){config.EnableCors();// Web API configuration and services// Configure Web API to use only bearer token authentication.config.SuppressDefaultHostAuthentication();config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));......


6. While implementing individual authentication using identity,There is one template available in visual studio inside provider folder ApplicationOAuthProvider.cs.in this file there is one method which grants resource credentials. The method name is:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)

In that method, one needs to check if some what like this is ever added.

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "yourdomain:yourpostnumber" });


In this way you can resolve cross domain issues easily.
Happy Coding!!

        

Saturday 7 January 2017

Post List of string from ajax to controller

Here is the proper way of posting a list of string from ajax to controller.

Ajax setup one way:
           var stringArray = new Array();
            stringArray[0] = "item1";
            stringArray[1] = "item2";
            stringArray[2] = "item3";
         
            $.ajax({
                type: "POST",
                url: '@Url.Action("Search", "Home")',
                data: JSON.stringify({ listkey: stringArray }),
            success: function (data) {
                alert(data.Result);
            },
            dataType: "json",           
            contentType: 'application/json; charset=utf-8',
             });

Ajax Setup second way:

 $.ajax({
                type: "POST",
                url: '@Url.Action("Search", "Home")',
                data: { listkey: stringArray },
            success: function (data) {
                alert(data.Result);
            },
            dataType: "json",
            traditional: true
             });

Now,Controller:
 public ActionResult Search(List<string> listkey)
        {
            var n = listkey;
            return Json(new { });
        }

Thanks,

Wednesday 3 August 2016

Extension method to calculate First/last day of month/week in c#

 public static partial class DateTimeExtensions
    {
        public static DateTime FirstDayOfWeek(this DateTime dt)
        {
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            return dt.AddDays(-diff).Date;
        }

        public static DateTime LastDayOfWeek(this DateTime dt)
        {
            return dt.FirstDayOfWeek().AddDays(6);
        }

        public static DateTime FirstDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

        public static DateTime LastDayOfMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1).AddDays(-1);
        }

        public static DateTime FirstDayOfNextMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1);
        }
    }


After declaring the above class ,you can use it like this:

 var firstdayofThisWeek = DateTime.Now.FirstDayOfWeek();


Happy Coding!

Tuesday 26 July 2016

DataTable Issue: Cannot set property '_DT_Cellindex' of undefined



First of all check the table structure:

Number of columns in ( th ) in thead section must equal number of columns (td) in tbody section.

Happy Coding!

Wednesday 13 April 2016

Send List of string using ajax on mvc controller

  var stringArray = new Array();
// or var stringArray=[];
 stringArray.push('1');
 stringArray.push('2');

var data = JSON.stringify({
                'AllRecord': stringArray
            });

 $.ajax({
                url: 'url',
                cache: false,
                traditional: true,
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: data,
                dataType: "json",
                success: function (data) {
                    // Code to be on success of this call
                }
            })

//In controller grab it like below

  public ActionResult SaveCompanyAndSendMailToAllUsers(List<String> AllAgents)
        {
               // your logic
        }