ASP.NET身份有點過於複雜,我會說。
2014年8月,他們宣佈了新版本2.1,事情又改變了。
首先,讓我們擺脫EntityFramework
:
Uninstall-Package Microsoft.AspNet.Identity.EntityFramework
現在我們執行我們自己的User
定義實現接口IUser
(Microsoft.AspNet.Identity):
public class User: IUser<int>
{
public User()
{
this.Roles = new List<string>();
this.Claims = new List<UserClaim>();
}
public User(string userName)
: this()
{
this.UserName = userName;
}
public User(int id, string userName): this()
{
this.Id = Id;
this.UserName = userName;
}
public int Id { get; set; }
public string UserName { get; set; }
public string PasswordHash { get; set; }
public bool LockoutEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool TwoFactorEnabled { get; set; }
public IList<string> Roles { get; private set; }
public IList<UserClaim> Claims { get; private set; }
}
,你可以請參閱我已經定義了我的Id
(int)的類型。
然後,您必須定義從Microsoft.AspNet.Identity.UserManager
繼承的自定義UserManager
,指定您的用戶類型和密鑰類型。
public class UserManager : UserManager<User, int>
{
public UserManager(IUserStore<User, int> store): base(store)
{
this.UserLockoutEnabledByDefault = false;
// this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(10);
// this.MaxFailedAccessAttemptsBeforeLockout = 10;
this.UserValidator = new UserValidator<User, int>(this)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
};
// Configure validation logic for passwords
this.PasswordValidator = new PasswordValidator
{
RequiredLength = 4,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
}
}
我已經在這裏實施了我的驗證規則,但如果您願意,您可以將它保留在外面。
UserManager
需要一個UserStore
(IUserStore)。
您將在此處定義您的DB邏輯。有幾個接口可以實現。但並非所有這些都是強制性的。
public class UserStore :
IUserStore<User, int>,
IUserPasswordStore<User, int>,
IUserLockoutStore<User, int>,
IUserTwoFactorStore<User, int>,
IUserRoleStore<User, int>,
IUserClaimStore<User, int>
{
// You can inject connection string or db session
public UserStore()
{
}
}
我沒有包括每個接口的所有方法。一旦你這樣做,你就可以寫你的新用戶:
public System.Threading.Tasks.Task CreateAsync(User user)
{
}
憑身份證取,
public System.Threading.Tasks.Task<User> FindByIdAsync(int userId)
{
}
等。
然後,您需要定義從Microsoft.AspNet.Identity.Owin.SignInManager
繼承的SignInManager
。
public class SignInManager: SignInManager<User, int>
{
public SignInManager(UserManager userManager, IAuthenticationManager authenticationManager): base(userManager, authenticationManager)
{
}
public override Task SignInAsync(User user, bool isPersistent, bool rememberBrowser)
{
return base.SignInAsync(user, isPersistent, rememberBrowser);
}
}
我只實現SignInAsync
:它會產生一個ClaimsIdentity。
就是這樣。
現在在您的Startup
類中,您必須告知Owin
如何創建UserManager
和SignInManager
。
app.CreatePerOwinContext<Custom.Identity.UserManager>(() => new Custom.Identity.UserManager(new Custom.Identity.UserStore()));
// app.CreatePerOwinContext<Custom.Identity.RoleManager>(() => new Custom.Identity.RoleManager(new Custom.Identity.RoleStore()));
app.CreatePerOwinContext<Custom.Identity.SignInService>((options, context) => new Custom.Identity.SignInService(context.GetUserManager<Custom.Identity.UserManager>(), context.Authentication));
我沒用過,你會在默認的模板,因爲我希望讓事情儘可能的簡單找到工廠。
而且使應用程序可以使用cookie:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<Custom.Identity.UserManager, Custom.Identity.User, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) =>
{
var userIdentity = manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
return (userIdentity);
},
getUserIdCallback: (id) => (Int32.Parse(id.GetUserId()))
)}
});
現在,在您的帳戶控制器 - 或控制器負責登錄 - 你必須得到UserManager
和SignInManager
:
public Custom.Identity.SignInManager SignInManager
{
get
{
return HttpContext.GetOwinContext().Get<Custom.Identity.SignInManager>();
}
}
public Custom.Identity.UserManager UserManager
{
get
{
return HttpContext.GetOwinContext().GetUserManager<Custom.Identity.UserManager>();
}
}
您將使用SignInManager
進行登錄:
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
和UserManager
創建用戶,添加角色和索賠:
if (ModelState.IsValid)
{
var user = new Custom.Identity.User() { UserName = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// await UserManager.AddToRoleAsync(user.Id, "Administrators");
// await UserManager.AddClaimAsync(user.Id, new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Country, "England"));
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
看來複雜...這是...種。
如果您想了解更多關於它的信息,請點擊這裏here和here。
如果你想運行一些代碼,看看它是如何工作的,我已經加入了一些code,它與Biggy一起工作(因爲我不想浪費很多時間來定義表格和類似的東西)。
如果您有機會從github repo下載我的代碼,您會注意到我已經創建了一個輔助項目(Custom.Identity),我已經將所有的東西都保存在了我的產品中。
唯一的NuGet包,你需要有:
- Microsoft.AspNet.Identity.Core
- Microsoft.AspNet.Identity.Owin
是的,你可以自定義的UserManager的行爲。它獲取數據的方式是通過實現'IUserStore'。這裏有一個答案可以幫助你解決如何自定義這個問題。 http://stackoverflow.com/questions/19940014/asp-net-identity-with-ef-database-first-mvc5/21122865#21122865 – Shoe