![](/img/trans.png)
[英]Using Windows Authentication in Blazor WebAssembly with .NET Core
[英]blazor webassembly windows authentication + .netcore hosted
Im still learning blazor and would like to make some solution in it, but requirment would be to use windows something like windows auth (user login is enough) i known that windows auth is not implemented/availible by now - will be in future?
所以场景是:
- 在浏览器中运行 wasm
-实施
public class MyAuthStateProvider: AuthenticationStateProvider
在
Task<AuthenticationState> GetAuthenticationStateAsync()
- 在那里获取 windows 用户名(如何??)
- 使用此用户名向 api 发布一些帖子,从 api 获取来自现有数据库的所有用户信息 + 用于下一次 api 调用的令牌
- 将其返回给 wasm 和 go。
有人可以指点我的方向吗? 我搜索了很多,但大多数解决方案是由于服务器端 blazor - 我需要它。 我还发现很多在服务器端实施新的自定义授权、自定义注册等 - 我已经拥有我的用户的数据库..
还是应该以完全不同的方式制作?
谢谢并恭祝安康
d00lar controller 对我不起作用,但这有效:
反而
return Ok(this.httpContextAccessor.HttpContext.User.Claims.Where(p => p.Type== ClaimTypes.Name).FirstOrDefault().Value.ToString());
我用了
[HttpGet]
[Authorize]
[Route("winuserid")]
public async Task<string> GetUserID()
{
string userid;
userid = User.Identity.Name;
return userid;
}
我也必须添加
app.UseAuthorization();
在服务器项目 Startup.cs
正如@Panagiotis-Kanavos 指出的那样,如果您将“托管”完整 WASM 应用程序(即选择“托管”选项的“Blazor Webassembly”项目)部署到 IIS,(使用“服务器”项目,而不是“客户端”项目),那么 Server 项目就变成了一个普通的 .NET Core 站点,其中包含一个 WASM 应用程序。
这实际上意味着您可以使用“服务器”项目来设置 Windows 身份验证。
Windows 身份验证在 WASM 项目上显示为灰色/不可用,因为 WASM 项目不是主机,它只是在浏览器中运行的程序集。
So, if you turn on Windows Auth on the deployed Server project, then you can use IIS authentication/process Windows claims the same way you would normally do for any .NET Core app. WASM 项目可能需要使用 .NET 核心服务器 API 来获取/接收/处理这些声明,但与实现自定义 Windows Auth 方案相比,开销很小。
即不需要实现自定义代码来打开它,只需在服务器项目上启用以下内容:
提醒:可以部署WASM 应用程序,但在这种情况下,这不是您想要的 - 您希望将“服务器”项目部署到 IIS - Visual Studio 包含自动为您在服务器项目中包装 WASM/客户端应用程序的逻辑。
好的,如果没有人知道,我会发布答案
在 SERVER 项目上,我们需要允许匿名 + windows 身份验证(在 iis 上只能是 windows - 仅在 Kestrel 中调试需要匿名)
同样在我们需要安装的服务器项目上
Microsoft.AspNetCore.Authentication.Negotiate
在ConfigureServices
我们需要添加services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
然后在一些 controller 例如
[Authorize]
[Route("getuser")]
public ActionResult Index()
{
return Ok(this.httpContextAccessor.HttpContext.User.Claims.Where(p => p.Type== ClaimTypes.Name).FirstOrDefault().Value.ToString());
}
所以我们需要在 wasm 中从这条路线获取,我们就完成了 - 这是如何在 Web 程序集中获取 windows 用户名
previous answer is a bit outdated so i created a git repo where i created sample app that use windows auth + authStateProvider + jwt for accessing api + swagger where we can also run getuser method, grab JWT token and paste as authorization for it. 在 .net 6
它并不完美,但很体面。 所以每个人都可以看到可能性并适应自己的需要
https://github.com/d00lar/BlazorWasmAuthStateProvicerWindowsAuthAndJWT
问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.