gpt4 book ai didi

c# - ASP.NET Membership 获取登录用户

转载 作者:行者123 更新时间:2023-11-30 14:38:31 25 4
gpt4 key购买 nike

我已经定义了一个用户名并将其添加到一个角色。

现在,我使用这段代码来检查用户是否有效:

if (Membership.ValidateUser(txtUsername.Text, txtPassword.Text)) {
FormsAuthentication.SetAuthCookie(txtUsername.Text, false);
if (Roles.GetRolesForUser(txtUsername.Text).Any(role => role == "Admin")) {
Page.Response.Redirect(ResolveUrl("~/Admin/Products.aspx"));
}
txtUsername.Text = "";
}

之后,我想检查 Products.aspx 页面中的用户是否在某个角色中。我写了这段代码,但它返回了我本地的 Windows 用户名:

Context.User.Identity.Name

我认为它应该返回登录用户。

之后我将检查此代码:

  if (!Context.User.IsInRole("Admin"))
{
Response.Redirect(ResolveUrl("~/Default.aspx"));

}


我的代码有什么问题?如果登录用户具有特定角色,我该如何检查?

最佳答案

您是否将authentication-mode设置为Forms

Web.config:

<authentication mode="Forms">
</authentication>

此外,您应该使用 Page.User.Identity.Name 而不是 Context.User.Identity.Name

关于c# - ASP.NET Membership 获取登录用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7863495/

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