Xax*_*xum 1 asp.net-profiles asp.net-mvc-4
我的web.conf文件如下:
<connectionStrings>
<add name="PaDBCon" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Pa.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
...
<profile>
<providers>
<clear />
<add name="ProfilesProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="PaDBCon"
applicationName="PaWeb" />
</providers>
<properties>
<add name="FirstName" type="String"/>
<add name="LastName" type="String"/>
<add name="Site" type="String"/>
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
但是我收到错误'找不到配置文件默认提供程序.' 如果我拿出提供者部分它创建一个aspnetdb.mdf,它不会在visual studio中打开,但我希望提供者与我的用户和角色一起存储在我的Pa.mdf数据库中.有关如何将配置文件保存在与用户帐户和角色相同的数据库中的任何想法?
编辑:使用时
<providers>
<clear />
<add name="ProfilesProvider"
type="System.Web.Providers.DefaultProfileProvider"
connectionStringName="PamperDBCon"
applicationName="PamperWeb" />
</providers>
Run Code Online (Sandbox Code Playgroud)
我收到错误 - 无法加载类型'System.Web.Providers.DefaultProfileProvider'.
编辑:作为下面的优秀答案的补充.此链接有助于添加更多说明.帮助澄清了我剩下的问题.
ASP.NET MVC 4使用新的SimpleMembershipProvider.以下是如何将自定义字段添加到自动生成的模板和用户配置文件中.
导航到该~/Models/AccountModel.cs文件并将字段添加到UserProfile该类:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
Run Code Online (Sandbox Code Playgroud)将新字段添加到RegisterModel视图模型:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
// New fields added to store profile information
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
Run Code Online (Sandbox Code Playgroud)修改~/Views/Account/Register.cshtml视图以允许用户在注册时输入这些附加字段:
@model MvcApplication1.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
</li>
<li>
@Html.LabelFor(m => m.Site)
@Html.TextBoxFor(m => m.Site)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Run Code Online (Sandbox Code Playgroud)更新AccountController中的Register操作,以便在创建新用户时存储此附加信息:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { FirstName = model.FirstName, LastName = model.LastName, Site = model.Site });
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Run Code Online (Sandbox Code Playgroud)点击Ctrl+F5以启动应用程序并创建一个新用户
UserProfile数据库中的表将填充其他字段.
最后,稍后您需要访问某些用户的属性(例如当前经过身份验证的用户):
using (var context = new UsersContext())
{
UserProfile profile = context.UserProfiles.First(x => x.UserName == User.Identity.Name);
// TODO: do something with the user profile
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2716 次 |
| 最近记录: |