简体   繁体   English

blazor webassembly windows 身份验证 + .netcore 托管

[英]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? 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?

So scenario would be:所以场景是:

-run wasm in browser - 在浏览器中运行 wasm

-implement -实施

  public class MyAuthStateProvider: AuthenticationStateProvider

in

  Task<AuthenticationState> GetAuthenticationStateAsync()

-get windows username there(how??) - 在那里获取 windows 用户名(如何??)

-make some post to api with this username, get from api all userinfo from existing DB + token for next api calls - 使用此用户名向 api 发布一些帖子,从 api 获取来自现有数据库的所有用户信息 + 用于下一次 api 调用的令牌

-return it to wasm and go. - 将其返回给 wasm 和 go。

Can someone please point me direction?有人可以指点我的方向吗? i searched alot but most of solutions was due to serwer-side blazor - i need it wasm.我搜索了很多,但大多数解决方案是由于服务器端 blazor - 我需要它。 I found also alot with implementing new custom authorization on server-side, custom registration etc - i already have database with my users..我还发现很多在服务器端实施新的自定义授权、自定义注册等 - 我已经拥有我的用户的数据库..

or should it be made totally different way?还是应该以完全不同的方式制作?

thanks and regards谢谢并恭祝安康

d00lar controller didn't work form me but this worked: d00lar controller 对我不起作用,但这有效:

instead反而

return Ok(this.httpContextAccessor.HttpContext.User.Claims.Where(p => p.Type== ClaimTypes.Name).FirstOrDefault().Value.ToString());

I used我用了

[HttpGet]
[Authorize]
[Route("winuserid")]
public async Task<string> GetUserID()
{
    string userid;
    userid = User.Identity.Name;
    return userid;
}

Also i had to add我也必须添加

       app.UseAuthorization();

in Server project Startup.cs在服务器项目 Startup.cs

As @Panagiotis-Kanavos pointed out, if you deploy a "hosted" full WASM app (ie "Blazor Webassembly" project with the "Hosted" option selected) to IIS, (using the "Server" project, not the "Client" project), then the Server project becomes just a normal .NET Core site with a WASM app wrapped inside.正如@Panagiotis-Kanavos 指出的那样,如果您将“托管”完整 WASM 应用程序(即选择“托管”选项的“Blazor Webassembly”项目)部署到 IIS,(使用“服务器”项目,而不是“客户端”项目),那么 Server 项目就变成了一个普通的 .NET Core 站点,其中包含一个 WASM 应用程序。

What this effectively means is that you can use the "Server" project to set Windows Authentication.这实际上意味着您可以使用“服务器”项目来设置 Windows 身份验证。

Windows Authentication is greyed out/not available on the WASM project because the WASM project is not a host it is simply an assembly running in the browser. 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. 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. The WASM project will likely need to use the .NET Core Server API to get/receive/process these claims, but that overhead is minimal compared to implementing a custom Windows Auth scheme. WASM 项目可能需要使用 .NET 核心服务器 API 来获取/接收/处理这些声明,但与实现自定义 Windows Auth 方案相比,开销很小。

Ie no need to implement custom code to turn it on, just enable the following on the Server project:即不需要实现自定义代码来打开它,只需在服务器项目上启用以下内容:

Blazor WebAssembly - 服务器项目属性

Reminder: The WASM app can be deployed but in this case that's not what you want - you want to deploy the "Server" project to IIS - Visual Studio has included logic that automatically wraps the WASM/Client app in the Server project for you.提醒:可以部署WASM 应用程序,但在这种情况下,这不是您想要的 - 您希望将“服务器”项目部署到 IIS - Visual Studio 包含自动为您在服务器项目中包装 WASM/客户端应用程序的逻辑。

ok so if nobody knows i will post answer好的,如果没有人知道,我会发布答案

on SERVER project we need to allow anonym + windows auth (on iis can be only windows - anonym is needed for debug in Kestrel only)在 SERVER 项目上,我们需要允许匿名 + windows 身份验证(在 iis 上只能是 windows - 仅在 Kestrel 中调试需要匿名)

also on server project we need to install同样在我们需要安装的服务器项目上

Microsoft.AspNetCore.Authentication.Negotiate

in ConfigureServices we need to add services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();ConfigureServices我们需要添加services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();

and then in some controller for example然后在一些 controller 例如

 [Authorize]
 [Route("getuser")]
 public ActionResult Index()
 {
        return Ok(this.httpContextAccessor.HttpContext.User.Claims.Where(p => p.Type== ClaimTypes.Name).FirstOrDefault().Value.ToString());
 }

so we need to get from this route in wasm and we are done - this is how to get windows username in Web assembly所以我们需要在 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. 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. in .net 6在 .net 6

it is not perfect but decent.它并不完美,但很体面。 so everyone can see possibilityes and adopt to own needs所以每个人都可以看到可能性并适应自己的需要

https://github.com/d00lar/BlazorWasmAuthStateProvicerWindowsAuthAndJWT https://github.com/d00lar/BlazorWasmAuthStateProvicerWindowsAuthAndJWT

regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM