使用OWIN和OpenID Connect,我很难理解我的代码是如何工作的。我从网上的一个样本中得到了这个。这是一个.Net 4.7.2应用程序。
相关OWIN启动代码:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
ClientId = System.Configuration.ConfigurationManager.AppSettings["clientID"],
ClientSecret = System.Configuration.ConfigurationManager.AppSettings["clientSecret"],
Authority = System.Configuration.ConfigurationManager.AppSettings["Authority"],
RedirectUri = System.Configuration.ConfigurationManager.AppSettings["redirectURI"],
Scope = System.Configuration.ConfigurationManager.AppSettings["scope"],
CallbackPath = new Microsoft.Owin.PathString(System.Configuration.ConfigurationManager.AppSettings["callbackURL"]),
RedeemCode = true,
SaveTokens = true,
ResponseType = OpenIdConnectResponseType.Code,
ResponseMode = OpenIdConnectResponseMode.Query
}
然后我有了这个带有登录功能的索引页面:
<form method="post" action="">
<div class="d-flex justify-content-center">
@*<button class="btn btn-primary mx-2" formaction="/Home/Login">Please Login!</button>*@
<input class="btn btn-primary mx-2" type="button" value="Login for access..." onclick="location.href='@Url.Action("Login", "Home")'" />
</div>
然后这个登录功能在家庭控制器:
[Authorize]
public ActionResult Login()
{
var user = User;
//_Claims = ClaimsPrincipal.Current.Identities.First().Claims.ToList();
string result = string.Empty;
if (User.Identity.IsAuthenticated)
{
//do something
}
}
不知怎的,我的"登录"按钮向我的身份提供者发出请求,一切都正常。但是在哪里以及如何?我没有做任何事情来触发它。我也没有使用任何Challenge((方法/机制。有人能解释一下或给我指一个详细的解释吗?提前感谢!
当授权处理程序看到具有[授权]属性的控制器并且用户未登录(未通过身份验证(时,它将自动向身份验证模块发送质询。它将依次查看添加到其中的处理程序(OpenIDConnect(。因此,它将自动联系您的身份提供商,该提供商将要求用户进行身份验证。最终,身份验证处理程序从IdentityServer(通过浏览器(获得一个请求,以便登录用户。