gpt4 book ai didi

c# - 此外,使用现有身份验证密码保护作为 Azure WebApp 托管的 ASP.NET Core MVC 网站

转载 作者:行者123 更新时间:2023-12-03 19:09:16 25 4
gpt4 key购买 nike

我有一个带有 ASP.NET Core Identity 的现有 ASP.NET Core MVC 应用程序,我在其中使用了 signInManager.PasswordSignInAsync 的组合。和 [Authorize]强制用户登录网站、具有特定角色等的属性。这在本地和 Azure WebApp 中都可以正常工作。
现在,我想将我的应用程序的预览版本发布到另一个 Azure WebApp。这一次,我希望每个访问者在显示网站上的任何内容之前输入另一组凭据。我想我想要类似 .htaccess/BasicAuthenication 的东西。然而,在用户输入第一组凭据后,他不应该登录,因为他应该需要使用正常的登录程序(就像在可公开访问的实时版本中一样,但它有某些页面需要用户登录)。基本上,我只想在顶部添加另一层密码保护不影响当前存在的身份验证 .
鉴于我希望允许使用预览密码的任何人访问,以下解决方案在我的情况下似乎不起作用:

  • 作为防火墙设置限制对 WebApp 的访问。客户端 IP 不会来自某个 IP 范围,它们将由其 ISP 动态分配。
  • 在前面使用带有 Azure AD 的个人用户帐户。这可能是我的后备(虽然我不确定如何准确实现)但我宁愿没有另一组个人用户凭据来照顾。凭据甚至可以像预览//预览一样简单。

  • 有没有一种简单的方法,比如在 Startup 类中添加两行代码来实现我想要的第二级密码保护?

    最佳答案

    您可以通过基本身份验证进行第二次身份验证,这很简单,不需要太多代码。您将需要一个在原始身份验证完成后拦截/调用的中间件
    中间件

    public class SecondaryBasicAuthenticationMiddleware : IMiddleware
    {
    //CHANGE THIS TO SOMETHING STRONGER SO BRUTE FORCE ATTEMPTS CAN BE AVOIDED
    private const string UserName = "TestUser1";
    private const string Password = "TestPassword1";

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
    //Only do the secondary auth if the user is already authenticated
    if (!context.User.Identity.IsAuthenticated)
    {
    string authHeader = context.Request.Headers["Authorization"];
    if (authHeader != null && authHeader.StartsWith("Basic "))
    {
    // Get the encoded username and password
    var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim();

    // Decode from Base64 to string
    var decodedUsernamePassword = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword));

    // Split username and password
    var username = decodedUsernamePassword.Split(':', 2)[0];
    var password = decodedUsernamePassword.Split(':', 2)[1];

    // Check if login is correct
    if (IsAuthorized(username, password))
    {

    await next.Invoke(context);
    return;
    }
    }

    // Return authentication type (causes browser to show login dialog)
    context.Response.Headers["WWW-Authenticate"] = "Basic";

    // Return unauthorized
    context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
    }
    else
    {
    await next.Invoke(context);
    }
    }

    //If you have a db another source you want to check then you can do that here
    private bool IsAuthorized(string username, string password) =>
    UserName == username && Password == password;
    }
    在启动 -> 配置(确保在现有身份验证和授权之后添加)
        //Enable Swagger and SwaggerUI
    app.UseMiddleware<SecondaryBasicAuthenticationMiddleware>(); //can turn this into an extension if you wish

    app.UseAuthentication();
    app.UseAuthorization();
    在 Startup -> ConfigureServices 中注册中间件
    services.AddTransient<SecondaryBasicAuthenticationMiddleware>();
    并且 chrome 应该弹出一个像这样的基本身份验证对话框
    enter image description here

    关于c# - 此外,使用现有身份验证密码保护作为 Azure WebApp 托管的 ASP.NET Core MVC 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62727471/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com