AccountController and SignOut method when using Microsoft.Identity.Web.UI
The signout html code located at
<li class="nav-item">
<a class="nav-link text-dark" asp-area="MicrosoftIdentity" asp-controller="Account" asp-action="SignOut">Sign out</a>
</li>
https://nuget.info/packages/Microsoft.Identity.Web.UI/2.12.4
Repository:
Type:
git
Url:https://github.com/AzureAD/microsoft-identity-web
Commit:
cd25e21816251470e78572821542e161878f0ab3
/// <summary>
/// Handles the user sign-out.
/// </summary>
/// <param name="scheme">Authentication scheme.</param>
/// <returns>Sign out result.</returns>
[HttpGet("{scheme?}")]
public IActionResult SignOut(
[FromRoute] string scheme)
{
if (AppServicesAuthenticationInformation.IsAppServicesAadAuthenticationEnabled)
{
if (AppServicesAuthenticationInformation.LogoutUrl != null)
{
return LocalRedirect(AppServicesAuthenticationInformation.LogoutUrl);
}
return Ok();
}
else
{
scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
return SignOut(
new AuthenticationProperties
{
RedirectUri = callbackUrl,
},
CookieAuthenticationDefaults.AuthenticationScheme,
scheme);
}
}
// C:\Users\clu\.nuget\packages\microsoft.identity.web.ui\2.12.4\lib\net5.0\Microsoft.Identity.Web.UI.dll
dnSpy反编译出来的源码
/// <summary>
/// Handles user sign in.
/// </summary>
/// <param name="scheme">Authentication scheme.</param>
/// <param name="redirectUri">Redirect URI.</param>
/// <returns>Challenge generating a redirect to Azure AD to sign in the user.</returns>
// Token: 0x06000013 RID: 19 RVA: 0x0000221C File Offset: 0x0000041C
[HttpGet("{scheme?}")]
public IActionResult SignIn([FromRoute] string scheme, [FromQuery] string redirectUri)
{
if (scheme == null)
{
scheme = "OpenIdConnect";
}
string redirectUri2;
if (!string.IsNullOrEmpty(redirectUri) && base.Url.IsLocalUrl(redirectUri))
{
redirectUri2 = redirectUri;
}
else
{
redirectUri2 = base.Url.Content("~/");
}
return this.Challenge(new AuthenticationProperties
{
RedirectUri = redirectUri2
}, new string[]
{
scheme
});
}
/// <summary>
/// Challenges the user.
/// </summary>
/// <param name="redirectUri">Redirect URI.</param>
/// <param name="scope">Scopes to request.</param>
/// <param name="loginHint">Login hint.</param>
/// <param name="domainHint">Domain hint.</param>
/// <param name="claims">Claims.</param>
/// <param name="policy">AAD B2C policy.</param>
/// <param name="scheme">Authentication scheme.</param>
/// <returns>Challenge generating a redirect to Azure AD to sign in the user.</returns>
// Token: 0x06000014 RID: 20 RVA: 0x0000227C File Offset: 0x0000047C
[HttpGet("{scheme?}")]
public IActionResult Challenge(string redirectUri, string scope, string loginHint, string domainHint, string claims, string policy, [FromRoute] string scheme)
{
if (scheme == null)
{
scheme = "OpenIdConnect";
}
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("claims", claims);
dictionary.Add("policy", policy);
Dictionary<string, object> parameters = new Dictionary<string, object>
{
{
"login_hint",
loginHint
},
{
"domain_hint",
domainHint
}
};
OAuthChallengeProperties oauthChallengeProperties = new OAuthChallengeProperties(dictionary, parameters);
if (scope != null)
{
oauthChallengeProperties.Scope = scope.Split(" ", StringSplitOptions.None);
}
oauthChallengeProperties.RedirectUri = redirectUri;
return this.Challenge(oauthChallengeProperties, new string[]
{
scheme
});
}
/// <summary>
/// Handles the user sign-out.
/// </summary>
/// <param name="scheme">Authentication scheme.</param>
/// <returns>Sign out result.</returns>
// Token: 0x06000015 RID: 21 RVA: 0x00002308 File Offset: 0x00000508
[HttpGet("{scheme?}")]
public IActionResult SignOut([FromRoute] string scheme)
{
if (!AppServicesAuthenticationInformation.IsAppServicesAadAuthenticationEnabled)
{
if (scheme == null)
{
scheme = "OpenIdConnect";
}
string redirectUri = base.Url.Page("/Account/SignedOut", null, null, base.Request.Scheme);
return this.SignOut(new AuthenticationProperties
{
RedirectUri = redirectUri
}, new string[]
{
"Cookies",
scheme
});
}
if (AppServicesAuthenticationInformation.LogoutUrl != null)
{
return this.LocalRedirect(AppServicesAuthenticationInformation.LogoutUrl);
}
return this.Ok();
}
signout之后跳转的页面
C:\workspace\GitHub\Microsoft\microsoft-identity-web\src\Microsoft.Identity.Web.UI\Areas\MicrosoftIdentity\Pages\Account\SignedOut.cshtml
@page
@model Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Pages.Account.SignedOutModel
@{
ViewData["Title"] = "Signed out";
}
<h2>@ViewData["Title"]</h2>
<p>
You have successfully signed out.
</p>
上面的signout走的是第二个分支
scheme ??= OpenIdConnectDefaults.AuthenticationScheme;
var callbackUrl = Url.Page("/Account/SignedOut", pageHandler: null, values: null, protocol: Request.Scheme);
return SignOut(
new AuthenticationProperties
{
RedirectUri = callbackUrl,
},
CookieAuthenticationDefaults.AuthenticationScheme,
scheme);
//
// Summary:
// Creates a Microsoft.AspNetCore.Mvc.SignOutResult with the specified authentication
// schemes and properties.
//
// Parameters:
// properties:
// Microsoft.AspNetCore.Authentication.AuthenticationProperties used to perform
// the sign-out operation.
//
// authenticationSchemes:
// The authentication scheme to use for the sign-out operation.
//
// Returns:
// The created Microsoft.AspNetCore.Mvc.SignOutResult for the response.
[NonAction]
public virtual SignOutResult SignOut(AuthenticationProperties properties, params string[] authenticationSchemes);
Assembly Microsoft.AspNetCore.Mvc.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\5.0.0\ref\net5.0\Microsoft.AspNetCore.Mvc.Core.dll
C:\workspace\GitHub\Microsoft\aspnetcore\src\Mvc\Mvc.Core\src\ControllerBase.cs
public virtual SignOutResult SignOut(AuthenticationProperties properties, params string[] authenticationSchemes)
=> new SignOutResult(authenticationSchemes, properties);
C:\workspace\GitHub\Microsoft\aspnetcore\src\Mvc\Mvc.Core\src\SignOutResult.cs
public SignOutResult(string authenticationScheme, AuthenticationProperties properties)
: this(new[] { authenticationScheme }, properties)
{
}
public override async Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (AuthenticationSchemes == null)
{
throw new InvalidOperationException(
Resources.FormatPropertyOfTypeCannotBeNull(
/* property: */ nameof(AuthenticationSchemes),
/* type: */ nameof(SignOutResult)));
}
var loggerFactory = context.HttpContext.RequestServices.GetRequiredService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger<SignOutResult>();
logger.SignOutResultExecuting(AuthenticationSchemes);
if (AuthenticationSchemes.Count == 0)
{
await context.HttpContext.SignOutAsync(Properties);
}
else
{
for (var i = 0; i < AuthenticationSchemes.Count; i++)
{
await context.HttpContext.SignOutAsync(AuthenticationSchemes[i], Properties);
}
}
}
The code you provided is a Razor Page written in the ASP.NET Core framework. Specifically, it's located in the SignedOut.cshtml
file within the Microsoft.Identity.Web.UI
project in the Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Pages.Account
directory.
Let me explain the components:
-
@page
: This directive indicates that this file is a Razor Page. Razor Pages are a feature in ASP.NET Core that allows you to define your UI and logic in a single Razor file. -
@model Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Pages.Account.SignedOutModel
: This specifies the model associated with this Razor Page. In this case, it's referencing theSignedOutModel
class from theMicrosoft.Identity.Web.UI
namespace. The model provides data and behavior for the page. -
@{ ... }
: This is a Razor code block. It's used for embedding C# code within the Razor Page. -
ViewData["Title"]
: This sets the value of the "Title" ViewData property. ViewData is a dictionary-like structure used to pass data from a controller to a view. -
<h2>@ViewData["Title"]</h2>
: This renders an HTML<h2>
element with the value of the "Title" ViewData property. -
<p>You have successfully signed out.</p>
: This renders a paragraph with the message "You have successfully signed out."
This Razor Page is responsible for displaying a confirmation message when a user successfully signs out of the application. It's part of the default UI provided by the Microsoft.Identity.Web.UI
package for handling authentication-related operations. The page is embedded in the Layout
of the application, meaning it's used as part of the overall user interface.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-08-03 在HearthRanger中使用Silverfish
2019-08-03 ReSharper “Cannot resolve symbol” even when project builds
2018-08-03 problem in Sourcetree
2018-08-03 Get started with Sourcetree
2015-08-03 Fixing common issues when hosting a .NET 4.0 WCF service in IIS 7
2015-08-03 一句话的设计模式
2015-08-03 wcf纯代码创建控制台应用