我实现了自定义身份验证,但如何通过应用程序获取 UserID

发布于 2024-12-02 05:02:17 字数 668 浏览 1 评论 0 原文

我根据本教程实现了自定义身份验证/授权 http://www.mattwrock.com/post/2009/10/14/Implementing-custom-Membership-Provider-and-Role-Provider-for-Authinticating-ASPNET-MVC-Applications.aspx< /a> 效果很好。我实现它是因为我不想在数据库中包含存储过程以及使用不同 RDBMS 的可能性。 但我这里有一个问题。我对用户进行身份验证,但我不知道如何将 UserId 存储在某处,因此当我需要根据 UserID 从数据库中获取某些内容时。比如:

List<Product> products = productsRepository.GetProductsByUserId(User.UserID);

如何做这个?

顺便说一句,还有比本教程中的方法更好的方法来进行自定义身份验证/授权吗?

谢谢

I implemented custom authentication/authorization based on this tutorial http://www.mattwrock.com/post/2009/10/14/Implementing-custom-Membership-Provider-and-Role-Provider-for-Authinticating-ASPNET-MVC-Applications.aspx
It works fine. I implemented it because I don't want to have stored procedures in my database and possibility to use different RDBMS.
But I have one issue here. I authenticate user but I don't know how to store UserId somewhere so when I need to get something from database based on UserID to get it. Something like:

List<Product> products = productsRepository.GetProductsByUserId(User.UserID);

How to make this?

BTW Is there any better way to make custom authentication/authorization than this from this tutorial?

Thanks

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

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

发布评论

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

评论(3

梦幻的心爱 2024-12-09 05:02:17

如果您实际上已经实现了所有方法,并且正在填充内置 MembershipUser,那么只需 Membership.GetUser().ProviderUserKey 将返回 UserId。

If you've actually implemented all the methods, and you're populating the built-in MembershipUser, then simply Membership.GetUser().ProviderUserKey will return ther UserId.

∞觅青森が 2024-12-09 05:02:17

在我的解决方案中我使用

 Docent docent = DocentRepo.GetByID(User.Identity.Name);

也许这对你有用

in my solution I use

 Docent docent = DocentRepo.GetByID(User.Identity.Name);

maybe this can be of use to you

幻梦 2024-12-09 05:02:17

如果您使用 FormsAuthentification,除了用户名之外,您还可以在 cookie/票证中编码一些自定义用户数据。但您必须手动创建 FormsAuthenticationTicket 并在登录期间将 UserData 属性设置为用户的 id。这样你就可以同时拥有用户名和用户名。用户身份。

        // during login
        var authCookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
        var ticket = FormsAuthentication.Decrypt(authCookie.Value);
        // preserve data in your configuration
        var ticketWithId = new FormsAuthenticationTicket(
                                version: ticket.Version,
                                name: ticket.Name,
                                issueDate: ticket.IssueDate,
                                expiration: ticket.Expiration,
                                isPersistent: ticket.IsPersistent,
                                userData: userId);

        authCookie.Value = FormsAuthentication.Encrypt(ticketWithId);
        _context.Response.Cookies.Add(authCookie);

然后你可以为 Controller 或 HttpContext 类提供一个扩展方法:

    public int? GetUserId(this Controller controller) {
        var identity = (FormsIdentity)controller.User.Identity;
        int id;
        if (int.TryParse(identity.Ticket.UserData, out id))
            return id;
        return null;
    }

但是如果你不需要 UserId 和 UserId ,您的用户的 UserName 数据,HttpContext.User.Identity.NameController.User.Identity.Name 将包含您当前用户的用户名

If you're using FormsAuthentification you can encode some custom user data in your cookie / ticket besides UserName. But you have to manually create a FormsAuthenticationTicket and set UserData property to the user's id during login. This way you can have both UserName & UserId.

        // during login
        var authCookie = FormsAuthentication.GetAuthCookie(userName, createPersistentCookie);
        var ticket = FormsAuthentication.Decrypt(authCookie.Value);
        // preserve data in your configuration
        var ticketWithId = new FormsAuthenticationTicket(
                                version: ticket.Version,
                                name: ticket.Name,
                                issueDate: ticket.IssueDate,
                                expiration: ticket.Expiration,
                                isPersistent: ticket.IsPersistent,
                                userData: userId);

        authCookie.Value = FormsAuthentication.Encrypt(ticketWithId);
        _context.Response.Cookies.Add(authCookie);

Then you can have an extension method for Controller or HttpContext classes:

    public int? GetUserId(this Controller controller) {
        var identity = (FormsIdentity)controller.User.Identity;
        int id;
        if (int.TryParse(identity.Ticket.UserData, out id))
            return id;
        return null;
    }

But if you don't need both UserId & UserName data for your user, than HttpContext.User.Identity.Name or Controller.User.Identity.Name will have the username for your current user

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