对于我的生活,我无法让SqlProfileProvider在我正在进行的MVC项目中工作.
我意识到的第一个有趣的事情是Visual Studio不会自动为您生成ProfileCommon代理类.这不是什么大问题,因为扩展ProfileBase类只是简单的事情.在创建了ProfileCommon类之后,我编写了以下用于创建用户配置文件的Action方法.
[AcceptVerbs("POST")]
public ActionResult CreateProfile(string company, string phone, string fax, string city, string state, string zip)
{
    MembershipUser user = Membership.GetUser();
    ProfileCommon profile = ProfileCommon.Create(user.UserName, user.IsApproved) as ProfileCommon;
    profile.Company = company;
    profile.Phone = phone;
    profile.Fax = fax;
    profile.City = city;
    profile.State = state;
    profile.Zip = zip;
    profile.Save();
    return RedirectToAction("Index", "Account"); 
}我遇到的问题是对ProfileCommon.Create()的调用无法转换为类型ProfileCommon,因此我无法取回我的配置文件对象,这显然导致下一行失败,因为配置文件为空.
以下是我的web.config的片段:
<profile defaultProvider="AspNetSqlProfileProvider" automaticSaveEnabled="false" enabled="true">
    <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
    <properties>
        <add name="FirstName" type="string" />
        <add name="LastName" type="string" …我在代码中实现了一个自定义的Profile对象,如Joel所述:
但是,当我创建一个新用户时,我无法让它工作.当我这样做:
Membership.CreateUser(userName, password);
Roles.AddUserToRole(userName, "MyRole");
用户是创建并添加到数据库中的角色,但HttpContext.Current.User仍然是空的,并Membership.GetUser()返回null,所以这(从Joel的代码)不起作用:
static public AccountProfile CurrentUser
{
    get { return (AccountProfile)
                     (ProfileBase.Create(Membership.GetUser().UserName)); }
}
AccountProfile.CurrentUser.FullName = "Snoopy";
我尝试过这样调用Membership.GetUser(userName)和设置Profile属性,但是set属性保持为空,并且调用AccountProfile.CurrentUser(userName).Save()不会在数据库中放置任何内容.我也试着指示用户是有效的和登录,通过调用Membership.ValidateUser,FormsAuthentication.SetAuthCookie等等,但是当前用户仍然是空的或匿名的(根据我的浏览器的cookie的状态).
解决(进一步编辑,见下文):根据Franci Penov的解释和一些更多实验,我找出了问题.Joel的代码和我尝试的变体只适用于现有的个人资料.如果不存在Profile,ProfileBase.Create(userName)则每次调用时都会返回一个新的空对象; 您可以设置属性,但它们不会"粘住",因为每次访问时都会返回一个新实例.设置HttpContext.Current.User一个新的GenericPrincipal 将会给你一个User对象,但没有一个Profile对象,ProfileBase.Create(userName)并且HttpContext.Current.Profile仍将指向新的,空的对象.
如果要在同一请求中为新创建的用户创建配置文件,则需要调用HttpContext.Current.Profile.Initialize(userName, true).然后,您可以填充初始化的配置文件并保存它,并且可以在将来的请求中按名称访问它,因此Joel的代码将起作用.我只在HttpContext.Current.Profile内部使用,当我需要在创建时立即创建/访问配置文件.在任何其他请求,我使用ProfileBase.Create(userName),并且我只公开该版本.
请注意,Franci是正确的:如果您愿意创建用户(和角色)并在第一次往返时将其设置为Authenticated,并要求用户再登录,您将能够更简单地访问该配置文件通过Joel的代码来处理后续请求.让我感到震惊的是,Roles可以在用户创建时立即访问,无需任何初始化,但Profile不是.
我的新AccountProfile代码:
public static AccountProfile CurrentUser
{
    get
    {
        if (Membership.GetUser() != null)
            return ProfileBase.Create(Membership.GetUser().UserName) as …问题: 由乔尔描述我创建了一个自定义配置文件对象这里.然后我使用Jeremy的方法(这里)扩展自定义配置文件,以允许我使用生成用户并设置这些值.然后,我创建了一个ViewModel来显示Memeberhip信息和配置文件信息,以便用户可以更新其会员信息(电子邮件)和配置文件信息.**视图显示我在视图中更新的信息中输入的所有字段,然后单击"保存",我收到以下错误
System.Configuration.SettingsPropertyNotFoundException:找不到设置属性"FirstName".**
这是我的自定义配置文件对象(模型):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using System.Web.Security;
namespace NDAC.Models
{
    public class ProfileInformation : ProfileBase
    {
        static public ProfileInformation CurrentUser
        {
            get
            {
                if (Membership.GetUser() != null)
                {
                     return (ProfileInformation)(ProfileBase.Create(Membership.GetUser().UserName));
                }
                else
                {
                    return null;
                }
            }
        }
        public virtual string FirstName {
            get
            {
                return ((string)(base["FirstName"]));
            }
            set 
            {
                base["FirstName"] = value; Save();
            } 
        }
        public virtual string LastName {
            get 
            {
                return ((string)(base["LastName"])); …我想realName在用户的用户名和电子邮件地址之外添加一个字段.
在过去,我创建了一个新表,其中包含用户注册时的username和realName列.但是,我想知道是否可以扩展默认的asp.net表单身份验证成员资格表或用户表(或者需要任何表来添加它),而不是为此信息创建新表.
问题:如何向身份验证表添加新列?
我们经常在asp.net webform项目中使用session,cache和profile.我们经常在asp.net webform项目中将数据存储在会话,缓存和配置文件中,但我想知道何时应该在会话中存储数据,或者何时应该存储在缓存和配置文件中.缓存或配置文件的范围是什么.这两个会话特定的生命周期或应用程序具体.
假设我是否在会话1中存储缓存或配置文件中的任何数据,那么我是否可以从session2访问该数据.当我们应该在会话,缓存和配置文件中存储数据时,请引导我使用场景和示例.谢谢
我正在ASP.NET MVC中编写一个具有用户帐户的站点.由于该网站将面向讨论,我认为我需要一个管理员系统,以便能够在Stack Overflow上调节用户,就像我们在这里一样.我希望能够将用户置于"暂停"状态,以便他们能够登录该站点(此时他们会收到一条消息,例如,"您的帐户已被暂停,直到[日期]"),但无法完成他们通常能够做的用户的功能.
实现这个的最佳方法是什么?
我正在考虑创建一个"暂停"角色,但问题是,我对普通用户本身有几个不同的角色,具有不同的权限.
你有没有设计过这样的功能?我该怎么办?提前致谢.
可能重复:
如何分配配置文件值?
我正在阅读一本ASP.NET书籍,如果您在启动项目时选择Web应用程序,则表明您无法使用Profile.它只能在网站下运行.
Web应用程序有其他替代方案吗?或者您是否需要构建自己的Profile系统.
我正在尝试将现有网站转换为Web应用程序项目,并且我在使配置文件工作时遇到很大问题.
网站项目中的代码隐藏的一个例子是
注册与 - 角色和profile.ascx.cs
    // Add the newly created user to the default Role.
    Roles.AddUserToRole(CreateUserWizard1.UserName, wsatDefaultRole);
    // Create an empty Profile for the newly created user
    ProfileCommon p = (ProfileCommon)ProfileCommon.Create(CreateUserWizard1.UserName, true);
    // Populate some Profile properties. Values are located in web.config file
    p.Company.Company = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeName")).Text;
    p.Company.Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeAddress")).Text;
    p.Company.City = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeCity")).Text;
    p.Company.State = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlStates")).SelectedValue;
    p.Company.PostalCode = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbOfficeZip")).Text;
    p.Company.Phone = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactPhone")).Text;
    p.Company.Fax = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txbContactFax")).Text;
    p.Preferences.Newsletter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlNewsletter")).SelectedValue;
    // Save profile - must be done since we explicitly created …asp.net ×4
asp.net-mvc ×4
c# ×2
profile ×2
c#-4.0 ×1
caching ×1
createuser ×1
membership ×1
profiles ×1
provider ×1
session ×1
viewmodel ×1
webforms ×1