ASP.Net MVC替代登录身份

时间:2015-08-08 14:44:14

标签: c# asp.net asp.net-mvc asp.net-identity owin

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

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

1 个答案:

答案 0 :(得分: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")
            });
    }
}

执行ConfigureAuth

的启动类
[assembly: OwinStartup(typeof(Startup))]

namespace Omu.ProDinner.WebUI
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            OwinConfig.ConfigureAuth(app);
        }
    }
}

以及您使用它的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");
    }
}

以下是您需要的软件包列表:

  <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" />
相关问题