zoukankan      html  css  js  c++  java
  • Asp.Net Identity自定义user类的运用,ClaimsIdentity

    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);
                    }
  • 相关阅读:
    围棋术语中英文对照
    修改grub及console的分别率 Linux-Ubuntu
    内核crash (Linux)
    pthread_create build
    内联函数定义的关键字inline及由此产生的编译问题简析
    debian家族重量级成员Ubuntu 20.04下载链接开启了。。。
    stm32 GPIO 输出配置参照
    Linux安装应用程序后,点击图标没法应,怎么解决呢?
    c语言中的引用使用
    QA Issue: PN: startUp is upper case, this can result in unexpected behavior. [uppercase-pn]
  • 原文地址:https://www.cnblogs.com/bushuosx/p/3955011.html
Copyright © 2011-2022 走看看