c# - ASP.Net MVC 替代登录身份

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

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

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

最佳答案

这是一个简单的 owin 身份验证实现,以防您仍想尝试一下: (这是从 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" />

关于c# - ASP.Net MVC 替代登录身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31894237/

相关文章:

c# - 按名称访问 DbSet 的属性

asp.net - 如何将下拉列表中的所有项目获取到gridview?

c# - ASP.NET MVC View 编译失败

c# - MVC 将 Base64 字符串转换为图像,但是...... System.FormatException

c# - 在发布模型中保留 SelectList

c# - .NET 反射中的转换

c# - 将多个文本属性写入一个 txt 文件

c# - 将 JSON 字符串反序列化为当前类

.net - 在 ASP.NET 应用程序中实现多语言的最佳方式

asp.net-mvc - window.location.href 发送多个参数 asp.net mvc