ASP.Net MVC替代登录身份

Ste*_*goo 8 c# asp.net asp.net-mvc owin asp.net-identity

阅读完教程并尝试后,我发现登录身份是一种错综复杂,不灵活的方式.更改为使用用户名并完全删除电子邮件是一场噩梦(我没有成功).这是我的经验,我没有力气继续沿着Owin Identity的方式前进.

ASP.Net社区可以接受OWIN/Identity登录的替代方案,就像OWIN/Identity一样有效吗?我有一个需要minimalistic的项目(用户名,密码,仅限全名).我希望我的问题不是开放式的,并且在SO限制范围内;)

Omu*_*Omu 7

这是一个简单的owin auth实现,如果你还想尝试一下:(它是从prodinner复制的代码)

你需要一个类来配置它:

public static class OwinConfig
{
    public static void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/SignIn")
            });
    }
}
Run Code Online (Sandbox Code Playgroud)

执行ConfigureAuth的启动类

[assembly: OwinStartup(typeof(Startup))]

namespace Omu.ProDinner.WebUI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            OwinConfig.ConfigureAuth(app);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以及AccountController你使用它的地方:

public class AccountController : Controller
{
    //... ctor, user service

    private IAuthenticationManager Authentication
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

    private void SignInOwin(string name, bool rememberMe, IEnumerable<string> roles)
    {
        var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, name) },
            DefaultAuthenticationTypes.ApplicationCookie,
                ClaimTypes.Name, ClaimTypes.Role);

        foreach (var role in roles)
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, role));
        }


        Authentication.SignIn(new AuthenticationProperties
        {
            IsPersistent = rememberMe
        }, identity);
    }

    public ActionResult SignIn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult SignIn(SignInInput input)
    {
        if (!ModelState.IsValid)
        {
            input.Password = null;
            input.Login = null;
            return View(input);
        }

        var user = userService.Get(input.Login, input.Password);

        if (user == null)
        {
            ModelState.AddModelError("", "incorrect username or password");
            return View();
        }

        SignInOwin(user.Login, input.Remember, user.Roles.Select(o => o.Name));


        return RedirectToAction("index", "home");
    }

    public ActionResult SignOff()
    {
        Authentication.SignOut();
        return RedirectToAction("SignIn", "Account");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是您需要的软件包列表:

  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net45" />
  <package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net45" />
  <package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net45" />
  <package id="Owin" version="1.0" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)