//put this middleware before the middleware which you want to check the user name
app.Use(async (context, next) => {
// Do work that can write to the Response.
if ( context.User.Identity != null)
{
var username = context.User.Identity.Name;
}
await next.Invoke();
// Do logging or other work that doesn't write to the Response.
});
1条答案
按热度按时间vh0rcniy1#
在我看来,我们不建议你从浏览器的弹出窗口中捕获js中的用户名。
当un-auth请求发送到服务器时会触发windows弹出窗口,在此期间,不会触发js,捕获用户输入不安全,可能包含风险。
如果你想获取用户名,一种可能的方法是从服务器端获取,并将其与响应一起发送给客户端js。
但根据您的描述,似乎您想根据用户名执行某些操作。
一种可能的方法是编写一个自定义中间件,以便在CORS中间件之前获取用户名。
关于如何从中间件获取,可以参考以下代码:
字符串
测试结果:
x1c 0d1x的数据