[英]Combine server-side and client-side authentication with WebAPI
我有一個遺留的ASP.NET webforms應用程序,用戶通過服務器端處理的表單登錄。 如果輸入的用戶名+密碼與數據庫中的憑證匹配,我會在會話中設置一些值(例如,當前用戶ID),然后執行Response.Redirect
。 我也創建了一個HttpCookie,用於“下次訪問時自動重新啟動”功能。
目前,我還在該Web應用程序中添加了WebApi支持。 我設法實現了令牌身份驗證,允許我在客戶端登錄。
如何結合兩種身份驗證方法? 我希望用戶輸入一次憑據,在服務器端進行身份驗證,在客戶端進行身份驗證后將用戶重定向到另一個頁面。
以下代碼將創建一個cookie以保持用戶登錄。
// 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);
}
您可以使用Angular JS在webapi中使用基於令牌的身份驗證。 請訪問以下鏈接http://www.dotnetcurry.com/aspnet/1223/secure-aspnet-web-api-using-tokens-owin-angularjs
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.