ASP .NET Core 2.0 - JWT外部身份验证

时间:2018-05-29 20:59:54

标签: c# asp.net-core oauth-2.0 asp.net-core-2.0 pingfederate

我正在尝试在ASP.NET Core 2.0 Web应用程序上开始身份验证。

我公司正在使用Ping Federate,我正在尝试使用公司登录页面对我的用户进行身份验证,并使用我的签名密钥(此处为X509SecurityKey来验证返回的令牌。)

登录页面链接如下所示:

https://companyname.com/authorization.oauth2?response_type=code&redirect_uri=https%3a%2f%2fJWTAuthExample%2fAccount%2fLogin&client_id=CompanyName.Web.JWTAuthExample&scope=&state=<...state...>

开箱即用,我将Startup.cs配置为能够登录并挑战此站点。

我用[Authorize(Policy="Mvc")]装饰了我的HomeController但是当我访问其中一个页面时,我只得到一个空白页。

当我将OnChallengeOnAuthenticationFailed方法添加到options.Events时,调试没有达到namespace JWTAuthExample { public class Startup { public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment) { Configuration = configuration; HostingEnvironment = hostingEnvironment; string certificatepath = Path.Combine(HostingEnvironment.ContentRootPath, $"App_Data\\key.cer"); KEY = new X509SecurityKey(new X509Certificate2(certificatepath)); } public IConfiguration Configuration { get; } public IHostingEnvironment HostingEnvironment { get; } private string AUTH_LOGINPATH { get; } = Configuration["DefaultAuth:AuthorizationEndpoint"]; private X509SecurityKey KEY { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { options.IncludeErrorDetails = true; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { // Ensure token expiry RequireExpirationTime = true, ValidateLifetime = true, // Ensure token audience matches site audience value ValidateAudience = false, ValidAudience = AUTH_LOGINPATH, // Ensure token was issued by a trusted authorization server ValidateIssuer = true, ValidIssuer = AUTH_LOGINPATH, // Specify key used by token RequireSignedTokens = true, IssuerSigningKey = KEY }; }); services.AddAuthorization(options => { options.AddPolicy("Mvc", policy => { policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); policy.RequireAuthenticatedUser(); }); }); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } } $n=$_COOKIE['idstudio']; for ($i=1;$i<$n;$i++) { echo "<table style='border: 1px solid black; margin-left:auto; margin- right:auto;'><caption> <p>Esperienze di Studio</p> </caption> <thead style='color:green;'> <tr>"; echo "<th style='border: 1px solid black;'> Titolo </th><th style='border: 1px solid black;'> Anno </th><th style='border: 1px solid black;'> Luogo </th> "; echo "</tr> </thead> <tbody style='color:blue'> <tr>"; echo "<td style='border: 1px solid black;'>".$_COOKIE["titolo$i"]." </td>"; echo "<td style='border: 1px solid black;'>".$_COOKIE["anno$i"]."</td>"; echo "<td style='border: 1px solid black;'>".$_COOKIE["luogo$i"]." </td>"; echo "</tr></tbody</table><br><br>"; } echo "</div>"; 方法(我认为因为用户需要先进行身份验证)。

那么,为了重定向到我的身份验证网站,我错过了什么?它是内置的还是我必须做一些手动配置?

(注意:在其他网络应用程序中,使用asp net framework,我在身份验证失败时在Authorize属性中使用重定向)

相关文章:Authorize attribute does not redirect to Login page when using .NET Core 2's AddJwtBearer - 从这篇文章来看,这是否意味着我没有使用正确的身份验证方法?我正在构建一个Web应用程序,而不是API。

$luogo = $_POST["luogo"];
$anno = $_POST["anno"];
$titolo = $_POST["titolo"];

$sql6="SELECT education_experience_id FROM education_experience ORDER BY 
education_experience_id DESC LIMIT 1;";
$result2=mysqli_query($connessione,$sql6);
$row2=mysqli_fetch_array($result2,MYSQLI_NUM);
$last_id2=$row2[0];
$id2 = $last_id2+1;

$eeid=$_COOKIE['id'];

$sql9="INSERT into EDUCATION_EXPERIENCE 
    (education_experience_id,title,year,place,user_id) VALUES                 
    ('$id2','$titolo','$anno','$luogo','$eeid')";
$res2=mysqli_query($connessione,$sql9);
echo "<br>";
mysqli_error($connessione);
if($res2 ==TRUE)   {
    setcookie("luogo$id2", $luogo, strtotime("+1 month"));
    setcookie("anno$id2", $anno, strtotime("+1 month"));
    setcookie("titolo$id2", $titolo, strtotime("+1 month"));
    setcookie("idstudio",$id2,strtotime("+1 month"));

    header("Location: Principale.php");
}
else
{
    echo "Impossibile aggiungere Esperienza<br>";
    echo "<a href='Principale.php'>Clicca Qui</a> per tornare alla pagina 
        principale.";
}

1 个答案:

答案 0 :(得分:0)

按照布拉德的建议,

以下是在ASP NET 2.0上执行OpenId Connect配置的代码示例

var ordered = list
    .OrderByDescending(x => x.IsRequired)
    .ThenBy(x => x.Index)
    .ThenBy(x=> x.AvailableDate)
    .ThenBy(x=> x.Key);

此处有更多详细信息:https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-2.1