Combine server-side and client-side authentication using WebAPI

I have an outdated ASP.NET web form application in which users register through a server-side form. If the entered username + password matches the credentials in the database, I set some values ​​in the sessions (for example, the current user ID) and then execute Response.Redirect . I also create an HttpCookie for the "automatically reassign me next time" function.

I am also currently adding WebApi support to this web application. I managed to implement token authentication, which allows me to log on to the client side.

How can I combine both authentication approaches? I want the user to enter his credentials once, authenticate on the server side and on the client side, redirecting users to another page after authentication.

+5
source share
2 answers

The following code will create a cookie so that the user logs in.

 // login etc if (chkRemember.Checked) { // calculate the total number of minutes in 20 days to use as the time out. int timeout = (int)TimeSpan.FromDays(30).TotalMinutes; // create an authentication ticket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(txtUserName.Text, true, timeout); // Encrypt the ticket string encrptedTicked = FormsAuthentication.Encrypt(ticket); // create the cookie for the ticket, and put the ticket inside HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrptedTicked); // give cookie and ticket same expiration cookie.Expires = ticket.Expiration; // Attach cookie to current response. it will now to the client and then back to the webserver with every request HttpContext.Current.Response.Cookies.Set(cookie); // send the user to the originally requested page. string requestedPage = FormsAuthentication.GetRedirectUrl(txtUserName.Text, false); Response.Redirect(requestedPage, true); } else { // login without saving cookie to client FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false); } 
0
source

You can use token-based authentication in webapi using Angular JS. Visit the following link http://www.dotnetcurry.com/aspnet/1223/secure-aspnet-web-api-using-tokens-owin-angularjs

-1
source

Source: https://habr.com/ru/post/1268275/


All Articles