Note that there are some explanatory texts on larger screens.

plurals
  1. POError :Request header field Content-Type is not allowed by Access-Control-Allow-Headers
    primarykey
    data
    text
    <p>I created an mvc4 web api project using vS2012. I used following tutorial to solve the Cross-Origin Resource Sharing, "http://blogs.msdn.com/b/carlosfigueira/archive/2012/07/02/cors-support-in-asp-net-web-api-rc-version.aspx". It is working successfully, and i post data from client side to server successfully.</p> <p>After that for implementing Autherization in my project, I used the following tutorial to implement OAuth2, "http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/18/oauth-2-0-for-mvc-two-legged-implementation.aspx". This is help me for getting RequestToken on client side.</p> <p>But when i post data from client side, i got the error, <strong>"XMLHttpRequest cannot load http://. Request header field Content-Type is not allowed by Access-Control-Allow-Headers."</strong></p> <p>My <strong>client side</strong> code look like,</p> <pre><code> function PostLogin() { var Emp = {}; Emp.UserName = $("#txtUserName").val(); var pass = $("#txtPassword").val(); var hash = $.sha1(RequestToken + pass); $('#txtPassword').val(hash); Emp.Password= hash; Emp.RequestToken=RequestToken; var createurl = "http://localhost:54/api/Login"; $.ajax({ type: "POST", url: createurl, contentType: "application/json; charset=utf-8", data: JSON.stringify(Emp), statusCode: { 200: function () { $("#txtmsg").val("done"); toastr.success('Success.', ''); } }, error: function (res) { toastr.error('Error.', 'sorry either your username of password was incorrect.'); } }); }; </code></pre> <p>My <strong>api controller</strong> look like,</p> <pre><code> [AllowAnonymous] [HttpPost] public LoginModelOAuth PostLogin([FromBody]LoginModelOAuth model) { var accessResponse = OAuthServiceBase.Instance.AccessToken(model.RequestToken, "User", model.Username, model.Password, model.RememberMe); if (!accessResponse.Success) { OAuthServiceBase.Instance.UnauthorizeToken(model.RequestToken); var requestResponse = OAuthServiceBase.Instance.RequestToken(); model.ErrorMessage = "Invalid Credentials"; return model; } else { // to do return accessResponse return model; } } </code></pre> <p>My <strong>webconfig</strong> file look like,</p> <pre><code> &lt;configuration&gt; &lt;configSections&gt; &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt; &lt;section name="oauth" type="MillionNodes.Configuration.OAuthSection, MillionNodes, Version=1.0.0.0, Culture=neutral"/&gt; &lt;sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth.Core"&gt; &lt;section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" /&gt; &lt;section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" /&gt; &lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;oauth defaultProvider="DemoProvider" defaultService="DemoService"&gt; &lt;providers&gt; &lt;add name="DemoProvider" type="MillionNodes.OAuth.DemoProvider, MillionNodes" /&gt; &lt;/providers&gt; &lt;services&gt; &lt;add name="DemoService" type="MillionNodes.OAuth.DemoService, MillionNodes" /&gt; &lt;/services&gt; &lt;/oauth&gt; &lt;system.web&gt; &lt;httpModules&gt; &lt;add name="OAuthAuthentication" type="MillionNodes.Module.OAuthAuthenticationModule, MillionNodes, Version=1.0.0.0, Culture=neutral"/&gt; &lt;/httpModules&gt; &lt;compilation debug="true" targetFramework="4.0" /&gt; &lt;authentication mode="Forms"&gt; &lt;forms loginUrl="~/Account/Login" timeout="2880" /&gt; &lt;/authentication&gt; &lt;pages&gt; &lt;namespaces&gt; &lt;add namespace="System.Web.Helpers" /&gt; &lt;add namespace="System.Web.Mvc" /&gt; &lt;add namespace="System.Web.Mvc.Ajax" /&gt; &lt;add namespace="System.Web.Mvc.Html" /&gt; &lt;add namespace="System.Web.Optimization" /&gt; &lt;add namespace="System.Web.Routing" /&gt; &lt;add namespace="System.Web.WebPages" /&gt; &lt;/namespaces&gt; &lt;/pages&gt; &lt;/system.web&gt; &lt;system.webServer&gt; &lt;validation validateIntegratedModeConfiguration="false" /&gt; &lt;modules&gt; &lt;add name="OAuthAuthentication" type="MillionNodes.Module.OAuthAuthenticationModule, MillionNodes, Version=1.0.0.0, Culture=neutral" preCondition="" /&gt; &lt;/modules&gt; &lt;httpProtocol&gt; &lt;customHeaders&gt; &lt;add name="Access-Control-Allow-Origin" value="*" /&gt; &lt;/customHeaders&gt; &lt;/httpProtocol&gt; &lt;/system.webServer&gt; &lt;dotNetOpenAuth&gt; &lt;messaging&gt; &lt;untrustedWebRequest&gt; &lt;whitelistHosts&gt; &lt;!-- Uncomment to enable communication with localhost (should generally not activate in production!) --&gt; &lt;!--&lt;add name="localhost" /&gt;--&gt; &lt;/whitelistHosts&gt; &lt;/untrustedWebRequest&gt; &lt;/messaging&gt; &lt;!-- Allow DotNetOpenAuth to publish usage statistics to library authors to improve the library. --&gt; &lt;reporting enabled="true" /&gt; </code></pre> <p> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload