我有一个MVC应用程序,我只有四个用户帐户。由于它们只是不会真正改变的管理帐户,所以我不想在ASP.NET MVC中处理数据库和所有成员选项。我只是需要一个非常简单的机制,以检查一些管理帐户,并授权用户(或不)。我知道这根本不是一个好的练习,但它非常适合我的情况:
有一个具有web的Web应用程序,所有(未经授权的)用户都通过Web与我的应用程序对话。有许多控制器仅供管理员使用,它们使用[Authorize]属性进行修饰。所有的网页管理员登录。该网站运行良好,除了我无法创建登录系统。每当我尝试登录(带有任何凭据)时,我都会收到The entity type ApplicationUser is not part of the model for the current context.错误消息。我没有ApplicationUser实体模型。我也从来没有过。我也不需要它。这发生在以下自动生成的行上:
var user = await UserManager.FindAsync(model.UserName, model.Password);如何摆脱默认的用户管理器和身份验证方法,并使用非常简单的身份验证方法?我在网上找到的所有文章都非常复杂。我一直在使用经典的ASP.NET,但我对ASP.NET MVC还很陌生,同时我开始了解ASP.NET MVC的工作原理和模式,我无法找到一个轻松的成员/授权起点。我只是只需要检查几个用户名和密码,而不需要数据库。
发布于 2014-01-27 04:12:37
假设您的表名为AppUser,则将您自己的AppUser域对象转换为IUser(使用Microsoft.AspNet.Identity),如下所示
using Microsoft.AspNet.Identity;
public class AppUser : IUser
{
//Existing database fields
public long AppUserId { get; set; }
public long AppUserName { get; set; }
public string AppPassword { get; set; }
public AppUser()
{
this.Id = Guid.NewGuid().ToString();
}
[Ignore]
public virtual string Id { get; set; }
[Ignore]
public string UserName
{
get { return AppUserName; }
set { AppUserName = value; }
}
}像这样实现UserStore对象(您必须定制UserStore对象以满足您的需求)。这里我展示了一个自定义的db上下文)
using Microsoft.AspNet.Identity;
public class UserStoreService :
IUserStore<AppUser>, IUserPasswordStore<AppUser>,
IUserSecurityStampStore<AppUser>
{
CompanyDbContext context = new CompanyDbContext();
public Task CreateAsync(AppUser user)
{
throw new NotImplementedException();
}
public Task DeleteAsync(AppUser user)
{
throw new NotImplementedException();
}
public Task<AppUser> FindByIdAsync(string userId)
{
throw new NotImplementedException();
}
public Task<AppUser> FindByNameAsync(string userName)
{
Task<AppUser> task =
context.AppUsers.Where(apu => apu.AppUserName == userName)
.FirstOrDefaultAsync();
return task;
}
public Task UpdateAsync(AppUser user)
{
throw new NotImplementedException();
}
public void Dispose()
{
context.Dispose();
}
public Task<string> GetPasswordHashAsync(AppUser user)
{
if (user == null)
{
throw new ArgumentNullException("user");
}
return Task.FromResult(user.AppPassword);
}
public Task<bool> HasPasswordAsync(AppUser user)
{
return Task.FromResult(user.AppPassword != null);
}
public Task SetPasswordHashAsync(AppUser user, string passwordHash)
{
throw new NotImplementedException();
}
public Task<string> GetSecurityStampAsync(AppUser user)
{
throw new NotImplementedException();
}
public Task SetSecurityStampAsync(AppUser user, string stamp)
{
throw new NotImplementedException();
}
}如果您有自己的自定义密码散列,则还需要实现IPasswordHasher.。下面是一个没有散列密码的例子(哦,不!)
using Microsoft.AspNet.Identity;
public class PasswordHasher : IPasswordHasher
{
public string HashPassword(string password)
{
return password;
}
public PasswordVerificationResult VerifyHashedPassword
(string hashedPassword, string providedPassword)
{
if (hashedPassword == HashPassword(providedPassword))
return PasswordVerificationResult.Success;
else
return PasswordVerificationResult.Failed;
}
}在Startup.Auth.cs替换中
UserManagerFactory = () =>
new UserManager<IdentityUser>(new UserStore<IdentityUser>());使用
var userManager = new UserManager<AppUser>(new UserStoreService());
userManager.PasswordHasher = new PasswordHasher();
UserManagerFactory = () => userManager;在ApplicationOAuthProvider.cs,中,将IdentityUser替换为、AppUser、,在AccountController.cs,中将IdentityUser替换为AppUser,并删除所有外部身份验证方法,如GetManageInfo和RegisterExternal等。
https://stackoverflow.com/questions/21371694
复制相似问题