標籤:style blog http color os io 使用 java ar
mvc5自動產生的使用者驗證是比較好用的,還可以擴充,可是要求code first,目前使用sqlite,支援entity framework,但不支援code first。
只有自已簡單模仿一下了。經過實驗,如下幾條後,可以運行了。
webconfig
<appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="owin:AutomaticAppStartup" value="true" /> </appSettings>
Startup.Auth.cs
public partial class Startup{ public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), });
}}
claim identity 相關介面,方法
public interface IUserIdentity { string Id { get; set; } string UserName { get; set; } } public static ClaimsIdentity CreateIdentity<TUserIdentity>(TUserIdentity user) where TUserIdentity : IUserIdentity { ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity")); identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); //identity.AddClaim(new Claim(ClaimTypes.PrimarySid, user.Id)); //identity.AddClaim(new Claim("DisplayName", user.UserName)); return identity; }
signinmanager
private IAuthenticationManager AuthenticationManager { get { return HttpContext.GetOwinContext().Authentication; } } var user = AppUserIdentityService.FindAsync(loginViewModel.UserName, loginViewModel.Password, db.MyUsers); if (user == null || user.Result == null) ModelState.AddModelError("", "使用者名稱或密碼不正確"); else { ClaimsIdentity identity = AppUserIdentityService.CreateIdentity(user.Result); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = loginViewModel.RememberMe }, identity); return RedirectToLocal(returnUrl); }
Asp.Net Identity自訂user類的運用,ClaimsIdentity