代码之家  ›  专栏  ›  技术社区  ›  Milan Raval

iPhone上的任何浏览器(Safari、Chrome或Edge)都没有提示进行Windows身份验证,需要如何调试此问题的帮助吗?

  •  0
  • Milan Raval  · 技术社区  · 2 年前

    我们有一个Asp-Net核心Web API服务器和Angular客户端。Asp-Net核心Web API已设置为执行Windows身份验证。此设置适用于PC、Mac和Android设备上的所有浏览器,并在下面显示输入windows用户和密码的登录提示。

    enter image description here

    但在iPhone上,没有任何浏览器提示用户和密码,因此Angular Client无法进行任何API调用。

    Angular Client:HttpInterceptor实现,确保凭据被传递到跨源web api。

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
          if ((this.authService.getConfig() !== undefined &&
              request.url.indexOf(this.authService.getConfig().logURL) === -1)) {
    
              const apiRoot = this.authService.getApiRoot();
              const authReq = request.clone({
                url: `${apiRoot}/${request.url}`,
                withCredentials: true 
              });
    
              return next.handle(authReq);
          } else {
              return next.handle(request);
          }
      }
    

    Web API已设置为协商Windows身份验证

    services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
    .AddNegotiate();
    

    IIS启用了Windows身份验证和匿名身份验证

    enter image description here

    如何调试此问题?

    0 回复  |  直到 2 年前
        1
  •  1
  •   Jason Pan    2 年前

    通过你的描述,以及我所做的调查 the explanations in this blog 。这似乎与iOS操作系统有关,在iOS操作系统中,windows身份验证在iPhone上受到了限制。

    完全可以从角度来判断浏览器的设备(比如 window.navigator.userAgent.toLowerCase(); ),如果发现它是iPhone设备,我们会显示我们自己创建的表单页面,然后对其进行身份验证。

    我的测试代码

    前端代码不是有角度的,它只是用于测试登录方法。

    @{
        ViewData["Title"] = "Login";
    }
    
    <h2>Login</h2>
    
    <form method="post" action="/WinLogin/Login">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required>
        <br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required>
        <br>
        <input type="submit" value="Login">
    </form>
    

    登录方法

    public class WinLoginController : Controller
    {
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
        int dwLogonType, int dwLogonProvider, out IntPtr phToken);
    
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }
    
        [HttpPost]
        public IActionResult Login(string username, string password)
        {
            IntPtr tokenHandle = new IntPtr(0);
            bool returnValue = LogonUser(username, ".", password, 2, 0, out tokenHandle);
    
            if (!returnValue)
            {
                int ret = Marshal.GetLastWin32Error();
                throw new Win32Exception(ret);
            }
    
            using (WindowsIdentity identity = new WindowsIdentity(tokenHandle))
            {
                var principal = new WindowsPrincipal(identity);
                HttpContext.User = principal;
                return RedirectToAction("Index", "Home");
            }
        }
    }
    

    测试结果

    enter image description here

    enter image description here