授权标签如何工作? - ASP.NET MVC

发布于 2024-08-03 02:37:40 字数 366 浏览 1 评论 0原文

授权标签如何判断用户是否获得授权?

例如,如果用户登录并尝试进入具有授权标签的视图。它如何判断用户是否被授权?它是否对数据库进行查询并检查?

如果他们进入具有角色授权的视图怎么样?它是否查询成员角色表?

我只是想知道,因为我有 ASP.NET 成员资格表认为重复的用户名。我使用一系列字段来确定哪个用户是什么,允许用户具有相同的重复用户名,但在我的数据库中仍然是唯一的。

这导致我必须为大量 .NET 成员资格内容编写自定义方法,因为它们都使用“userName”而不是使用 UserId 进行搜索。

所以我现在想知道授权标签是否会出现这种情况。因为我不知道它是如何工作的,并且如果我不使用 .NET 成员身份,我也不知道它将如何确定它。

How does the Authorize Tag determine if the user is authorized or not?

Like say, if a user logs in and they try to go to a view that has an Authorize tag. How does it determine if a user is authorized or not? Does it do a query to database and check?

How about if they go to a view with a role authorization? Does it query the membership role table?

I am just wondering since I have what the ASP.NET membership tables considers duplicate userNames. I use a serious of fields to determine which user is what, allowing users to have the same duplicate userName, but still be unique in my database.

This caused me to have to write custom methods for lots of .NET membership stuff since it all used "userName" to do searching instead of using the UserId.

So I am now wondering if this could be the case with the Authorize tag. Since I have no clue how it works and like if I was not using .NET membership I would not have a clue how it would determine it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-08-10 02:37:40

Authorize 标记使用 ASP.NET 中的所有内置成员资格检查。滚动自己的标签非常容易。例如:

public class MyAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) throw new ArgumentNullException("httpContext");

        // Make sure the user is authenticated.
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // Do you own custom stuff here
        bool allow = CheckIfAllowedToAccessStuff();

        return allow;
    }
}

然后您可以使用 [MyAuthorize] 标记,该标记将使用您的自定义检查。

The Authorize tag uses all the built in membership checks from ASP.NET. It's VERY easy to roll your own tag. For example:

public class MyAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (httpContext == null) throw new ArgumentNullException("httpContext");

        // Make sure the user is authenticated.
        if (httpContext.User.Identity.IsAuthenticated == false) return false;

        // Do you own custom stuff here
        bool allow = CheckIfAllowedToAccessStuff();

        return allow;
    }
}

You then can use the [MyAuthorize] tag which will use your custom checks.

旧情勿念 2024-08-10 02:37:40

ControllerActionInvoker 解析属性并调用 OnAuthorization () 当需要检查凭据时。

AuthorizationAttribute.OnAuthorization() 方法主要检查 User.Identity.IsAuthenticated 是否为 true。这只是利用了 FormsAuthentication 或您可能使用的任何其他身份验证方案的功能。

ControllerActionInvoker parses the attribute and calls OnAuthorization() on it when it's time to check the credentials.

The AuthorizationAttribute.OnAuthorization() method basically checks to see if User.Identity.IsAuthenticated is true or not. This just draws on the functionality of FormsAuthentication or whatever other authentication scheme you may be using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文