asp.net mvc 自定义配置文件提供程序

发布于 2024-11-10 01:53:43 字数 2498 浏览 6 评论 0原文

我想在我的 asp.net mvc 3 应用程序中拥有自定义配置文件提供程序。问题是,我不想使用 ASP.NET 成员资格/角色/配置文件提供程序生成的默认数据库,主要是因为身份验证已经使用 WebService 完成,并且数据库已经存在。

我想要用户配置文件属性来填充它们并在网站的不同区域中使用。

我查看了这个示例(如何分配配置文件值?),但我是收到此错误:

尝试为文件附加自动命名数据库 C:\Projects\FWManager\App_Data\aspnetdb.mdf 失败。一个数据库 存在同名文件,或者指定的文件无法打开,或者是 位于 UNC 共享上。

这是 web.config

  <profile inherits="FWMembership.Membership.FWProfileProvider" defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
   <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
   </providers>
  </profile>

这是我的自定义类

public class FWProfileProvider : ProfileBase
{
    [SettingsAllowAnonymous(false)]
    public string FirstName
    {
        get { return base["FirstName"] as string; }
        set { base["FirstName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string LastName
    {
        get { return base["LastName"] as string; }
        set { base["LastName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public int? UserID
    {
        get { return base["UserID"] as int?; }
        set { base["UserID"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string UserCompany
    {
        get { return base["UserCompany"] as string; }
        set { base["UserCompany"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string Email
    {
        get { return base["Email"] as string; }
        set { base["Email"] = value; }
    }

    public StringCollection Entitlements
    {
        get { return base["Entitlements"] as StringCollection; }
        set { base["Entitlements"] = value; }
    }

    public string username;

    public FWProfileProvider()
    {
    }

    public FWProfileProvider(string username)
    {
        this.username = username;
    }

    static public FWProfileProvider CurrentUser
    {
        get
        {
            return (FWProfileProvider)
                   (ProfileBase.Create("Joe"));
        }
    }

}

关键是避免使用 asp.net 默认成员资格表。 有什么想法吗?

编辑: 忘记添加 - 此 Web 应用程序,但配置文件提供程序放置在同一解决方案中的类库项目中: 解决方案 |->FWProfile(类库项目) |->UI(asp.net mvc 3 Web 应用程序)

I want to have custom profile provider in my asp.net mvc 3 app. The problem is, that I don't want to use default DB that is generated by ASP.NET Membership/Role/Profile provider, mainly because authentication is already done with WebService and DBs already exist.

I want to user profile properties to populate them and use within different areas of the site.

I took a look at this example (How to assign Profile values?) but I am getting this error:

An attempt to attach an auto-named database for file
C:\Projects\FWManager\App_Data\aspnetdb.mdf failed. A database with
the same name exists, or specified file cannot be opened, or it is
located on UNC share.

Here is the web.config

  <profile inherits="FWMembership.Membership.FWProfileProvider" defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
   <providers>
    <clear/>
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
   </providers>
  </profile>

This is my custom class

public class FWProfileProvider : ProfileBase
{
    [SettingsAllowAnonymous(false)]
    public string FirstName
    {
        get { return base["FirstName"] as string; }
        set { base["FirstName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string LastName
    {
        get { return base["LastName"] as string; }
        set { base["LastName"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public int? UserID
    {
        get { return base["UserID"] as int?; }
        set { base["UserID"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string UserCompany
    {
        get { return base["UserCompany"] as string; }
        set { base["UserCompany"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    public string Email
    {
        get { return base["Email"] as string; }
        set { base["Email"] = value; }
    }

    public StringCollection Entitlements
    {
        get { return base["Entitlements"] as StringCollection; }
        set { base["Entitlements"] = value; }
    }

    public string username;

    public FWProfileProvider()
    {
    }

    public FWProfileProvider(string username)
    {
        this.username = username;
    }

    static public FWProfileProvider CurrentUser
    {
        get
        {
            return (FWProfileProvider)
                   (ProfileBase.Create("Joe"));
        }
    }

}

The key is to avoid using asp.net default membership tables.
Any ideas?

EDIT:
Forgot to add - this web application, but profile provider is placed in the class library project within same soulution:
Solution
|->FWProfile (class library project)
|->UI (asp.net mvc 3 web application)

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

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

发布评论

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

评论(1

弄潮 2024-11-17 01:53:43

我认为您还必须编写自己的会员提供程序。您的 web.config 引用默认的 asp.net 成员资格提供程序。如何编写会员资格提供程序,您可以在此处找到 自定义会员资格提供程序

默认会员资格提供程序使用连接字符串到本地安装的 SQL Express 数据库并导致错误。

您的 web.config 看起来像这样:

<membership defaultProvider="MyCustomMembershipProvider">
  <providers>
    <clear />
    <add name="MyCustomMembershipProvider"
      type="FWMembership.Membership.MyCustomMembershipProvider"
      enablePasswordRetrieval="true"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="false"
      requiresUniqueEmail="true"
      passwordFormat="Clear"/>
  </providers>
</membership>
<profile defaultProvider="MyProfileProvider" enabled="true">   
   <providers>    
      <clear/>    
         <add name="MyProfileProvider" type="FWMembership.Membership.FWProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />   
   </providers>  
 </profile>

希望这有帮助。

I think you have to write your own MemberShip Provider as well. Your web.config refers to the default asp.net membership provider. How to write a Membership provider you can find here custom membership provider

The default membership provider uses a connection string into a locally installed SQL Express database and that causes your error.

Your web.config would look like this:

<membership defaultProvider="MyCustomMembershipProvider">
  <providers>
    <clear />
    <add name="MyCustomMembershipProvider"
      type="FWMembership.Membership.MyCustomMembershipProvider"
      enablePasswordRetrieval="true"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="false"
      requiresUniqueEmail="true"
      passwordFormat="Clear"/>
  </providers>
</membership>
<profile defaultProvider="MyProfileProvider" enabled="true">   
   <providers>    
      <clear/>    
         <add name="MyProfileProvider" type="FWMembership.Membership.FWProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />   
   </providers>  
 </profile>

Hope this helps.

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