我正在使用Entity Framework 5 Database First方法开发MVC 5 Web应用程序.我正在使用OWIN进行用户身份验证.下面显示我的帐户控制器中的登录方法.
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
if (user != null)
{
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));
identity.AddClaim(new Claim(ClaimTypes.GivenName, "A Person"));
identity.AddClaim(new Claim(ClaimTypes.Sid, user.userID)); //OK to store userID here?
AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = model.RememberMe
}, identity);
return RedirectToAction("Index", "MyDashboard");
}
else
{
ModelState.AddModelError("", "Invalid username or password."); …
Run Code Online (Sandbox Code Playgroud) asp.net authentication asp.net-mvc claims-based-identity asp.net-mvc-5
我正在使用Entity Framework 4.1开发一个ASP.Net MVC 3 Web应用程序,我对使用数据注释进行表单验证感到困惑.我总是将ViewModel返回给View而不是传递实际对象,因为我意识到这是不好的做法.例如:
public class ViewModelTeam
{
public Team Team { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的观点可能会有这样的东西
@model UI.ViewModels.ViewModelTeam
@Html.HiddenFor(model => model.Team.teamID)
<div class="editor-label">
@Html.LabelFor(model => model.Team.description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Team.description)
@Html.ValidationMessageFor(model => model.Team.description)
</div>
Run Code Online (Sandbox Code Playgroud)
为了验证这个View,我在部分类中创建了数据注释,就像这样
[MetadataType(typeof(TeamMetaData))]
public partial class Team
{
public class TeamMetaData
{
[DisplayName("Team Name")]
[Required(ErrorMessage = "Please enter a Team Name")]
public object description { get; set; }
Run Code Online (Sandbox Code Playgroud)
然后在我的创建控制器中我有这个
[HttpPost]
public ActionResult Create(Team team)
{
if (ModelState.IsValid)
{
//Add team and redirect …
Run Code Online (Sandbox Code Playgroud) 我最近把Live使用MVC 4和Entity Framework 5构建的Web应用程序.该MVC应用程序使用剃刀意见.
我注意到使用Elmah,当用户登录应用程序时,有时他们会收到以下错误
The provided anti-forgery token was meant for user "" but the current user is "user"
Run Code Online (Sandbox Code Playgroud)
我已经就如何解决这个问题做了一些研究,但似乎没有什么对我有用.请参阅下面的我的登录视图和相应的控制器操作.
剃刀视图
@if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="formEl_a">
<fieldset>
<legend>Login Information</legend>
<div class="lbl_a">
Email
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email, new { @class = "inpt_a" })<br />
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="lbl_a">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field sepH_b">
@Html.PasswordFor(m => …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个ASP.Net MVC 4 Web应用程序.以前我的MVC应用程序是使用MVC 3开发的,而这个新的MVC 4应用程序我刚刚复制/重用了以前应用程序的身份验证和授权代码.
当用户登录我的网站时,我会执行以下操作
账户管理员
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid)
{
User user = _userService.GetUser(model.Email.Trim());
//Create Pipe Delimited string to store UserID and Role(s)
var userData = user.ApplicantID.ToString();
foreach (var role in user.UserRoles)
{
userData = userData + "|" + role.description;
}
_formAuthService.SignIn(user.ApplicantFName, false, userData);
return RedirectToAction("Index", "Portfolio");
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
FormsAuthenticationService
public class FormsAuthenticationService : IFormsAuthenticationService
{
public void SignIn(string userName, bool createPersistentCookie, string …
Run Code Online (Sandbox Code Playgroud) 我正在使用Entity Framework Database First方法和现有数据库开发MVC 5 Web应用程序.
我也使用ASP.Net Identity进行授权和身份验证,但是,我没有使用内置的Entity Framework代码,即UserManager,ApplicationUser等,而是我使用的方法与Brock Allen类似.
我现在正在进行帐户登录和注册,我想在将用户密码存储到自定义用户表之前对其进行哈希处理.
我意识到我可以创建自己的自定义类来实现IPasswordHasher,但是,这就是我陷入困境的地方.下面显示了我认为它应该如何工作的模拟,但是,我并不完全确定这是正确的.
public class CustomPassword : IPasswordHasher
{
public string HashPassword(string password)
{
return password;
}
public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
{
if (hashedPassword.Equals(providedPassword))
return PasswordVerificationResult.Success;
else return PasswordVerificationResult.Failed;
}
}
Run Code Online (Sandbox Code Playgroud)
这些是我的问题:
Q1:当我注册一个新的用户帐户并将我的帐户控制器中的用户密码传递给HashPassword方法时,我希望将用户密码哈希并作为字符串返回,但是,我不知道要放什么代码进入HashPassword 函数来做到这一点.
CustomPassword pwd = new CustomPassword();
String UserPassword = "test@123";
String HashedNewPassword = pwd.HashPassword(UserPassword);
Run Code Online (Sandbox Code Playgroud)
Q2:当用户登录网站时,我想提取他们提供的密码,从数据库用户表中检索哈希密码,然后在 …
我正在使用Razor Views开发ASP.Net MVC 3 Web应用程序.我有以下ViewModel,它传递给我的Razor View并迭代以显示记录列表.
视图模型
public class ViewModelLocumEmpList
{
public IList<FormEmployment> LocumEmploymentList {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
视图
<table>
<tr>
<th>Employer</th>
<th>Date</th>
</tr>
@foreach (var item in Model.LocumEmploymentList) {
<tr>
<td>@item.employerName</td>
<td>@item.startDate</td>
</tr>
}
</table>
Run Code Online (Sandbox Code Playgroud)
我的问题是这条线
@Html.DisplayFor(modelItem => item.startDate)
Run Code Online (Sandbox Code Playgroud)
返回这样的日期20/06/2012 00:00:00,我希望它删除时间,只显示日期,即20/06/2012.
我试过添加
@Html.DisplayFor(modelItem => item.startDate.Value.ToShortDateString())
Run Code Online (Sandbox Code Playgroud)
和
DisplayFor(modelItem => item.startDate.HasValue ? item.startDate.Value.ToShortDateString(): "")
Run Code Online (Sandbox Code Playgroud)
但是,它们都在运行时返回以下错误消息
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Run Code Online (Sandbox Code Playgroud)
我在这里看了Darin Dimitrov的回答使用razor转换DateTime格式
但是,我无法访问ViewModel中的startDate属性,我的ViewModel返回一个Formistmployment对象的IList,您可以在上面看到它.
如果有人对如何从日期时间属性中删除时间有任何想法,那么我将非常感激. …
我已经在Stackoverflow上阅读了很多关于在包含业务层的ASP.Net MVC 3应用程序中使用工作单元模式的帖子.但是,关于这个话题,我仍然有几个问题,非常感谢人们给我的任何反馈.
我正在开发一个使用EF 4.1的ASP.Net MVC 3 Web应用程序.我将使用这个项目的存储库和工作单元模式,类似于在这个伟大的教程中使用它们的方式
我的项目的不同之处在于我还需要包含一个业务层(我的解决方案中的单独项目),以便为应用程序执行各种业务规则.上面提到的教程没有Business层,因此从控制器创建了一个Unit of Work类的实例
public class CourseController : Controller
{
private UnitOfWork unitOfWork = new UnitOfWork();
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是,如果我有业务层,我应该在哪里创建工作单元类的实例?
我个人认为它应该在我的控制器中创建,然后注入到业务层中,如下所示:
public class PeopleController : Controller
{
private readonly IUnitOfWork _UoW;
private IPersonService _personService;
public PeopleController()
{
_UoW = new UnitOfWork();
_personService = new PersonService(_UoW);
}
public PeopleController(IUnitOfWork UoW, IPersonService personService)
{
_UoW = UoW;
_personService = personService;
}
public ActionResult Edit(int id)
{
Person person = _personService.Edit(id);
return View(person);
}
public class UnitOfWork …
Run Code Online (Sandbox Code Playgroud) 我在ASP.Net MVC 3 Razor View中有以下内容
@foreach (var item in Model.FormNotes) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.User.firstName)
</td>
</tr>
}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但是,我想连接字符串以显示firstName和lastName,但是当我尝试这样做时
<td>
@Html.DisplayFor(modelItem => item.User.firstName + @item.User.lastName)
</td>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式
有谁知道如何在Razor视图中连接字符串?
谢谢大家.
编辑
我的Razor View接受一个看起来像这样的ViewModel
public class ViewModelFormNoteList
{
public IList<Note> FormNotes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想将FullName属性放在这里,正如Roy所建议的那样,但是,我不知道如何让它工作?
我正在创建一个新的Web应用程序,将使用MVC 5和Entity Framework Database First Approach编写.我还想使用ASP.Net Identity来管理会员资格,身份验证,授权等.
我已经阅读了很多关于网络上的ASP.Net身份以及它是如何工作的,但是,我仍然在学习这个主题.
当我在Visual Studio 2013中创建我的MVC 5应用程序并查看帐户控制器时,我的第一直觉是我不喜欢我所看到的,即,正在引用名为' ApplicationDbContext ' 的DbContext.我不喜欢这个的原因是因为我更喜欢将我的DbContext保留在我的解决方案中的适当项目中,即在我的模型层中,它遵循关注点逻辑的分离.
此外,开箱即用的MVC 5项目使用Entity Framework Code First创建默认数据库和表来存储用户,角色等.
因为我必须使用现有数据库和现有的User表,所以这种方法不适合我的需要.
我仍然希望为我的应用程序使用最新的ASP.Net标识,因为它看起来有很多好处,因此,我发现这篇文章剥离了很多实体框架代码,但仍然将OWIN驱动的身份验证纳入ASP.NET MVC.
http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux
使用上面的教程,这是我的帐户控制器的HttpPost登录方法
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
//Calling my own custom Account Service which validates users login details
var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
if (user)
{
var identity = …
Run Code Online (Sandbox Code Playgroud) 在阅读了JQGrid控件之后,我决定在我的一个ASP.Net MVC 3 Web应用程序中使用它.
首先,我按照Phil Haacks教程http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx这一切都很好.然后我尝试在我的应用程序中实现类似的东西,唯一的区别是,我使用Linq To Entities.
我的View页面已经导入了所有的css和Jquery类,然后我有我的JavaScript函数和保存数据的表
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/LinqGridData/',
datatype: 'json',
mtype: 'GET',
colNames: ['equipmentID', 'categoryTitle', 'title'],
colModel: [
{ name: 'equipmentID', index: 'equipmentID', width: 40, align: 'left' },
{ name: 'categoryTitle', index: 'categoryTitle', width: 40, align: 'left' },
{ name: 'title', index: 'title', width: 200, align: 'left'}],
pager: jQuery('#pager'),
width: 660,
height: 'auto',
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/scripts/themes/coffee/images',
caption: …
Run Code Online (Sandbox Code Playgroud) razor ×4
asp.net-mvc ×3
architecture ×1
asp.net ×1
c# ×1
datetime ×1
jqgrid ×1
jquery ×1
owin ×1
unit-of-work ×1
viewmodel ×1