ASP.Net MVC 按区域成员资格

发布于 2024-08-31 06:27:12 字数 241 浏览 5 评论 0原文

我正在构建一个 ASP.Net MVC 应用程序,它将在共享托管帐户上运行以托管多个域。我从包含成员资格的默认模板开始,并为每个域创建了一个 mvc 区域。路由设置为根据请求所针对的域指向正确的区域。现在我想设置特定于每个 mvc 区域的成员资格。我首先尝试了显而易见的方法,并尝试覆盖每个区域的 web.config 部分以更改提供程序的 applicationName 属性。这不起作用,因为该区域未设置为应用程序根目录。有没有一种简单的方法来区分每个区域的用户?

I am building an ASP.Net MVC app that will run on a shared hosting account to host multiple domains. I started with the default template that includes membership and created an mvc area for each domain. Routing is set up to point to the correct area depending on the domain the request is for. Now I would like to set up membership specific to each mvc area. I tried the obvious first and attempted to override the section of the web.config for each area to change the applicationName attribute of the provider. That doesn't work since the area is not set up as an application root. Is there an easy way to separate the users for each area?

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

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

发布评论

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

评论(1

我恋#小黄人 2024-09-07 06:27:12

我想我有一个可行的解决方案,可以使每个区域完全分开。使用默认模板作为起点,我向 MvcApplication1.Models.AccountMembershipService 类添加了另一个构造函数以接受字符串(还修改了现有构造函数以消除歧义)。

    public AccountMembershipService()
    {
        _provider = Membership.Provider;
    }

    public AccountMembershipService(MembershipProvider provider)
    {
        _provider = provider ?? Membership.Provider;
    }

    public AccountMembershipService(string applicationName)
        : this()
    {
        _provider.ApplicationName = applicationName;
    }

然后,我将 AccountController 复制到每个区域中,并修改了初始化重载以包含路线数据中的区域名称。

    protected override void Initialize(RequestContext requestContext)
    {
        if (FormsService == null) { FormsService = new FormsAuthenticationService(); }            
        if (MembershipService == null) { MembershipService = new   AccountMembershipService(requestContext.RouteData.DataTokens["area"].ToString()); }

        base.Initialize(requestContext);
    }

现在,每个区域都在表单身份验证下注册为新应用程序,并且所有用户和角色应保持独立。

I think I have a working solution that keeps each area completely separate. Using the default template as a starting point I added another constructor to the MvcApplication1.Models.AccountMembershipService class to accept a string (Also modified the existing constructors to eliminate ambiguity).

    public AccountMembershipService()
    {
        _provider = Membership.Provider;
    }

    public AccountMembershipService(MembershipProvider provider)
    {
        _provider = provider ?? Membership.Provider;
    }

    public AccountMembershipService(string applicationName)
        : this()
    {
        _provider.ApplicationName = applicationName;
    }

Then I copied the AccountController into each area and modified the Initialize overload to include the area name from the route data.

    protected override void Initialize(RequestContext requestContext)
    {
        if (FormsService == null) { FormsService = new FormsAuthenticationService(); }            
        if (MembershipService == null) { MembershipService = new   AccountMembershipService(requestContext.RouteData.DataTokens["area"].ToString()); }

        base.Initialize(requestContext);
    }

Now each area is registered as a new application under forms authentication and all users and roles should be kept separate.

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