奇怪的 RoleProvider 行为

发布于 2024-08-17 19:47:42 字数 2186 浏览 3 评论 0原文

我使用 DataContext 设置了一个角色提供者,如下所示。在我的视图中,我有以下用户控件...

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        <%= Html.ActionLink("Logout", "Logout", "Membership") %> | (
        <%
            var roles = System.Web.Security.Roles.GetRolesForUser(Page.User.Identity.Name);
            foreach (string role in roles)
                Html.Encode(role);
         %>
         )

现在,如果我在本地计算机上运行它,它可以正常工作。请注意,连接到与我的远程盒子相同的数据库。它登录并显示该成员所属的角色。

如果我在远程机器上运行相同的数据库,它将登录并显示成员名称 - 但是......它不会呈现角色。有什么想法为什么会发生这种情况吗?我的角色提供者如下所示。

using System;
using System.Linq;

using System.Collections;
using System.Collections.Generic;

namespace Providers
{
    public partial class RoleProvider : System.Web.Security.RoleProvider
    {
        private readonly Models.Contexts.DatabaseDataContext dataContext;

        protected Models.Contexts.DatabaseDataContext DataContext
        {
            get { return dataContext; }
        }

        public RoleProvider()
            : base()
        {
            if (dataContext == null)
                dataContext = new DataContextProvider().GetDataContext<Models.Contexts.DatabaseDataContext>("contextProvider");
        }

        public RoleProvider(IDataContextProvider dataContextProvider)
            : this()
        {
            dataContext = dataContextProvider.GetDataContext<Models.Contexts.DatabaseDataContext>("contextProvider");
        }

        public override string[] GetRolesForUser(string email)
        {
            DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
            var member = (
                from m in DataContext.Users
                where m.Email == email
                select m).Single();

            List<string> roleList = new List<string>();
            foreach (Models.Group group in member.Groups)
                roleList.Add(group.Role.RoleName);

            return roleList.ToArray<string>();
        }
}

I have a roleprovider set up as follows using a DataContext. On my View, I have the following user Control...

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        <%= Html.ActionLink("Logout", "Logout", "Membership") %> | (
        <%
            var roles = System.Web.Security.Roles.GetRolesForUser(Page.User.Identity.Name);
            foreach (string role in roles)
                Html.Encode(role);
         %>
         )

Now if I run this on my local machine, it works fine. Mind you, connected to the same database as my remote box. It logs in, and shows the roles the member is a part of.

if I run on the remote box , with the SAME database, it will login and show the member name - BUT... it doesn't render the roles. Any ideas why this might be happening? My Role Provider is drawn out below.

using System;
using System.Linq;

using System.Collections;
using System.Collections.Generic;

namespace Providers
{
    public partial class RoleProvider : System.Web.Security.RoleProvider
    {
        private readonly Models.Contexts.DatabaseDataContext dataContext;

        protected Models.Contexts.DatabaseDataContext DataContext
        {
            get { return dataContext; }
        }

        public RoleProvider()
            : base()
        {
            if (dataContext == null)
                dataContext = new DataContextProvider().GetDataContext<Models.Contexts.DatabaseDataContext>("contextProvider");
        }

        public RoleProvider(IDataContextProvider dataContextProvider)
            : this()
        {
            dataContext = dataContextProvider.GetDataContext<Models.Contexts.DatabaseDataContext>("contextProvider");
        }

        public override string[] GetRolesForUser(string email)
        {
            DataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
            var member = (
                from m in DataContext.Users
                where m.Email == email
                select m).Single();

            List<string> roleList = new List<string>();
            foreach (Models.Group group in member.Groups)
                roleList.Add(group.Role.RoleName);

            return roleList.ToArray<string>();
        }
}

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

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

发布评论

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

评论(1

任谁 2024-08-24 19:47:42

老实说,我会首先查看数据库中的数据差异,这听起来很愚蠢,但也许角色实际上并不存在,或者 ApplicationId 不同。如有必要,使用不同的数据库对应用程序运行 SQL Profiler,并验证查询是否相同以及返回的数据是否相同。

I would honestly start by looking at the data differences in the databases, it sounds dumb but maybe the roles don't actually exist or ApplicationIds are different. If necessary run SQL Profiler against the app using the different databases and verify the queries are the same and the data returned is the same.

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