![](/img/trans.png)
[英]Check if user is logged in with Token Based Authentication in ASP.NET Core
[英]How to check if user is logged in or not in forms based authentication
我想在我的網站上手動實現基於表單的身份驗證。
我正在使用Web.config
文件進行數據存儲
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx"
name=".ASPXAUTH"
path="/"
requireSSL="false"
slidingExpiration="true"
defaultUrl="~/Admin/OrderHistory.aspx"
cookieless="UseDeviceProfile"
enableCrossAppRedirects="false"
>
<credentials passwordFormat="Clear">
<user name="Admin" password="adm123$"/>
<user name="Administrator" password="adm234%"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users ="?" />
<allow users = "*" />
</authorization>
在根目錄下有一個Login.aspx
頁面,使用ASP.NET登錄控件來獲取用戶名和密碼。
一切正常,但當用戶logged in
並手動轉到login.aspx
頁面時,它不會將用戶重定向到defaultUrl頁面。
我想將用戶重定向到特定頁面/ defaultUrl頁面,如果他已登錄並手動登錄到login.aspx頁面
怎么做?
if (FormsAuthentication.Authenticate(LoginUser.UserName, LoginUser.Password))
{
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);
}
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
//Redirect to Default page
Response.Redirect("default.aspx");
}
在web.config文件中使用forms屬性defaulturl =“xyz.aspx”(您的URL)
protected void Page_Load(object sender, EventArgs e)
{
if (!Request.UrlReferrer.Equals("http://localhost:1360/login_page.aspx"))
{
Response.Redirect("ERROR ==>? 404");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.