ClientFormsIdentity.IsAuthenticated 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出使用者是否已經過驗證。
public:
property bool IsAuthenticated { bool get(); };
public bool IsAuthenticated { get; }
member this.IsAuthenticated : bool
Public ReadOnly Property IsAuthenticated As Boolean
屬性值
如果使用者已經驗證,則為 true
,否則為 false
。
實作
範例
下列範例程式碼示範如何透過 IIdentity 參考來使用此屬性,以判斷使用者目前是否已針對用戶端應用程式服務進行驗證。 此範例假設應用程式位於預設設定中,使用者不需要在驗證 Cookie 到期時再次登入。 否則, WebException 可能表示使用者登入已過期。
private void SaveSettings()
{
System.Security.Principal.IIdentity identity =
System.Threading.Thread.CurrentPrincipal.Identity;
// Return if the user is not authenticated.
if (identity == null || !identity.IsAuthenticated) return;
// Return if the authentication type is not "ClientForms".
// This indicates that the user is not authenticated for
// client application services.
if (!identity.AuthenticationType.Equals("ClientForms")) return;
try
{
Properties.Settings.Default.Save();
}
catch (System.Net.WebException)
{
MessageBox.Show("Unable to access the Web settings service. " +
"Settings were not saved on the remote service.",
"Not logged in", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Private Sub SaveSettings()
Dim identity As System.Security.Principal.IIdentity = _
System.Threading.Thread.CurrentPrincipal.Identity
' Return if the user is not authenticated.
If identity Is Nothing OrElse Not identity.IsAuthenticated Then Return
' Return if the authentication type is not "ClientForms". This indicates
' that the user is not authenticated for client application services.
If Not identity.AuthenticationType.Equals("ClientForms") Then Return
Try
My.Settings.Save()
Catch ex As System.Net.WebException
MessageBox.Show("Unable to access the Web settings service. " & _
"Settings were not saved on the remote service.", _
"Not logged in", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
備註
您通常會存取 ClientFormsIdentity 物件做為 IIdentity 參考,以避免直接相依于這個類別。 您可以藉由檢查 IIdentity.IsAuthenticated 身分識別的 屬性來判斷使用者是否已通過驗證。 不過,使用者可能會針對 Windows 進行驗證,但不適用於用戶端應用程式服務。 若要判斷使用者是否已針對用戶端應用程式服務進行驗證,您也應該確認 IIdentity.AuthenticationType 屬性值為 「ClientForms」。 如需詳細資訊,請參閱 類別概 ClientFormsIdentity 觀。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應