简体   繁体   中英

Redirect to Login.aspx Page from Web-Api

I have a Login.aspx page where I use Forms Authentication to login to the site. I use a cookie to store the additional user data and in the next page I use Asynchronous web-api calls to get some data. In the process I read the cookie and the user data. If the user data is found I proceed and if not I want to logout the user and redirect them to login page. I have the following code but the execution of the web-api call continues.

    FormsAuthentication.SignOut();
    FormsAuthentication.RedirectToLoginPage();
    HttpContext.Current.Response.End();

How do I make the user log out and redirect to login page when I know that the cookie is not found?

You should check for the cookie before you make the Web API calls.

  • If the cookie exists and is valid, call the service.
  • If the cookie doesn't, redirect to the login page.
  • In your API, check for the cookie in the call. If the cookie does not exist, return a 401 unauthorized response code in your web api response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM