Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

Sending Authorized Requests from WebForms to MVC

In a recent blog entry, we explained how to call an MVC Controller method from ASPX code-behind code. The demo didn't use any authorization and therefore, all methods were accessible anonymously. When an authorization method is used, the HttpWebRequest won't be authorized and will be routed to the specified MVC login page. As most applications are using an authorization method, there are two ways of solving this: Usage of the AllowAnonymousAttribute This attribute marks controllers and…

Sending Authorized Requests from WebForms to MVC

In a recent blog entry, we explained how to call an MVC Controller method from ASPX code-behind code. The demo didn't use any authorization and therefore, all methods were accessible anonymously.

When an authorization method is used, the HttpWebRequest won't be authorized and will be routed to the specified MVC login page. As most applications are using an authorization method, there are two ways of solving this:

  1. Usage of the AllowAnonymousAttribute

    This attribute marks controllers and actions to skip the authorization. With this attribute, the Controller method would look like this:

    [HttpPost]
    [AllowAnonymous]
    public bool SaveTemplate(string document)
    {
        // these are the values coming from the HTTP Post action
        // of the ASPX page
        string doc = document;
    
        // the document can now be saved in your Controller action
        // ...
    
        // return true, if successful
        return true;
    }
  2. Sending the AuthCookie with the request

    In this case, the authorization cookie will be sent with the WebRequest to authorize the request. The following code shows how to request the AuthCookie in order to add a newly created cookie to the HttpWebRequest:

    Uri uri = new Uri("http://" +
        Request.Url.Authority + "/Home/SaveTemplate");
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    
    // get the AuthCookie
    var authCookie =
        FormsAuthentication.GetAuthCookie(User.Identity.Name, true);
    
    // create a new Cookie
    Cookie requestAuthCoockie = new Cookie()
    {
        Expires = authCookie.Expires,
        Name = authCookie.Name,
        Path = authCookie.Path,
        Secure = authCookie.Secure,
        Value = authCookie.Value,
        Domain = uri.Host,
        HttpOnly = authCookie.HttpOnly,
    };
    
    // add the AuthCookie to the WebRequest
    request.CookieContainer = new CookieContainer();
    request.CookieContainer.Add(requestAuthCoockie);

    It is important to mention that FormsAuthentication must be enabled in this scenario:

    <system.web>
      <authentication mode="Forms"/>
    </system.web>

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


ASP.NETDocument EditorHTML5

Implementing Conditional Table Cell Colors with MailMerge

This ASP.NET MVC sample shows how to implement conditional table cell colors using the online document editor and an ASP.NET backend.


ASP.NETAzureHTML5

Deploying the MVC HTML5 Editor to Azure App Services

This article describes how to deploy the ASP.NET MVC HTML5 editor to Azure App Services.


HTML5MVCRelease

Sneak Peek X15: Copy to Local Clipboard Support in ASP.NET Version

The HTML5 based editor for ASP.NET MVC and Web Forms (AJAX) provides access to two different clipboards: The server-side clipboard uses the internal TX Text Control format and allows to copy and…


ReportingHTML5MVC

Sneak Peek X15: Custom Field Overlays in HTML5-based Text Control

A highly requested feature for our HTML5-based Web.TextControl is the ability to show field overlays or custom elements such as HTML form elements on top of all types of fields. In version X15,…