Skip to content

Commit

Permalink
Update: OnRedirectToLogin check ApiControllerAttribute
Browse files Browse the repository at this point in the history
Test: add swagger
  • Loading branch information
shps951023 committed Apr 24, 2024
1 parent f21e7cc commit e85aa79
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
5 changes: 2 additions & 3 deletions src/MiniAuth.IdentityAuth/MiniAuthIdentityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ MiniAuthIdentityDbContext _dbContext
await SaveUser(context, _dbContext, userManager);
}).RequireAuthorization("miniauth-admin");

// /api/resetPassword
endpoints.MapPost("/miniauth/api/resetPassword", async (HttpContext context
, ILogger<MiniAuthIdentityEndpoints> _logger
, MiniAuthIdentityDbContext _dbContext
Expand All @@ -111,7 +110,7 @@ MiniAuthIdentityDbContext _dbContext

private static string GetNewPassword()
{
return $"MiniAuth@{Guid.NewGuid().ToString().Substring(0, 10)}";
return $"{Guid.NewGuid().ToString().Substring(0, 10).ToUpper()}@{Guid.NewGuid().ToString().Substring(0, 5)}";
}
private async Task ResetPassword(HttpContext context, MiniAuthIdentityDbContext _dbContext, UserManager<MiniAuthIdentityUser> userManager)
{
Expand Down Expand Up @@ -294,7 +293,7 @@ private async Task Login(HttpContext context, ILogger<MiniAuthIdentityEndpoints>
{
_logger.LogInformation("User logged in.");
var newToken = Guid.NewGuid().ToString();
context.Response.Cookies.Append("X-MiniAuth-Token", newToken);
//context.Response.Cookies.Append("X-MiniAuth-Token", newToken);
await OkResult(context, $"{{\"X-MiniAuth-Token\":\"{newToken}\"}}");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
Expand Down Expand Up @@ -73,12 +74,12 @@ public static IServiceCollection AddMiniIdentityAuth(this IServiceCollection ser
{
OnRedirectToLogin = ctx =>
{

if (ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
var routeEndpoint = ctx.HttpContext.GetEndpoint();
var isJsonApi = ctx.Request.Headers["X-Requested-With"] == "XMLHttpRequest" ||
routeEndpoint.Metadata?.GetMetadata<Microsoft.AspNetCore.Mvc.ApiControllerAttribute>() != null;
if (isJsonApi)
{
#if DEBUG
Debug.WriteLine($"IsXMLHttpRequest Path: {ctx.Request.Path}");
#endif
ctx.Response.StatusCode = 401;
}
else
Expand Down
5 changes: 4 additions & 1 deletion tests/MiniAuth.Identity/MiniAuth.IdentityWeb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
</PropertyGroup>

<ItemGroup>

<ProjectReference Include="..\..\src\MiniAuth.IdentityAuth\MiniAuth.IdentityAuth.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>


</Project>
14 changes: 14 additions & 0 deletions tests/MiniAuth.Identity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using Microsoft.AspNetCore.Identity;
using MiniAuth.IdentityAuth.Models;

namespace MiniAuth.Identity
{
public class Program
Expand All @@ -8,10 +11,21 @@ public static void Main(string[] args)
builder.Services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
//builder.Services.AddControllers();
builder.Services.AddMiniIdentityAuth();
#if DEBUG
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
#endif

var app = builder.Build();
app.UseCors("AllowAll");
app.MapGet("/", () => "Hello World!");
//app.MapGroup("/api").MapIdentityApi<IdentityUser>();
//app.MapControllers();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.Run();
}
}
Expand Down

0 comments on commit e85aa79

Please sign in to comment.