问题描述
使用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()方法/机制。有人可以解释或指向我详细解释吗?预先感谢!
解决方法
当授权处理程序看到具有[Authorize]属性的控制器并且用户未登录(未认证)时,它将自动向质询模块发送质询。依次查看添加了哪些处理程序(OpenIDConnect)。因此,它将随后自动与您的身份提供者联系,后者将要求用户进行身份验证。最终,身份验证处理程序从IdentityServer(通过浏览器)获取一个请求,以便它可以登录用户。