Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have web method, I only want functionality of this method to run if the user is authorized user only but if not then I want to redirect the web method call to unauthorizedpage.

How do I do it ?

I cant use this, since web method is static.
Response.Redirect("~/NotAuthorized.aspx")


What I have tried:

If oUser.IsAuthorizedMyAppUser = False Then
                       Response.Redirect("~/NotAuthorized.aspx")
        End If
Posted
Updated 3-Dec-19 21:27pm

<script type="text/javascript">
      function Logout() {
          $.ajax({
              type: "POST",
              url: "test.aspx/mymethod",
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({}),
              async: false,
              dataType: "json",
              success: function (data) {
                  window.location.href = data.d;
              },
              error: function (e) {
                  alert('Error in method Process is : ' + e.statusText);
              }
          });
          return false;
      }
  </script>
 
Share this answer
 
Comments
CHill60 4-Dec-19 3:41am    
An unexplained code dump is not a solution. You have not explained why your method is better than the accepted solution from 2 years ago. Nor have you tried to help the OP by pointing out what to do. Very poor.
You'll need to handle this in your calling code by returning data from your method that will tell your js that it needs to redirect;

[WebMethod]
public static object MyMethod()
{
    return new { Success = false, RedirectUrl = VirtualPathUtility.ToAbsolute("~/NotAuthorized.aspx")};
}


Your js would then be something like

$.ajax({
    type: "POST",
    url: "WebForm1.aspx/MyMethod",
    contentType: "application/json",
    data: {},
    dataType: "json",
    success: function (data) {
        var r = data.d;
        if (r.Success == false){
            window.location.href = r.RedirectUrl;
        }
    }
 
Share this answer
 
Comments
[no name] 29-Jul-17 0:50am    
Thank you sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900