Asp.Net Identity自訂user類的運用,ClaimsIdentity

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.