ASP.Net MVC替代登录身份



在阅读教程和试用后,我发现登录身份是复杂的,不灵活的方式。改变使用用户名和完全删除电子邮件是一个噩梦(我没有成功)。这就是我的经历,我没有力量继续走自己的身份之路了。

是否有OWIN/Identity登录的替代方案是ASP可以接受的?网络社区就像OWIN/身份一样有效?我有一个项目,需要简约(用户名,密码,全名ONLY)。我希望我的问题不是开放式的,在SO范围内;)

这里有一个简单的自己的验证实现,如果你仍然想尝试一下:(它的代码复制自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" />

相关内容

  • 没有找到相关文章

最新更新