• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Asp.NetIdentity自定义user类的运用,ClaimsIdentity

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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);
                }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap