首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义Auth在Asp上使用OWIN

自定义Auth在Asp上使用OWIN
EN

Stack Overflow用户
提问于 2016-06-08 14:25:43
回答 1查看 1.5K关注 0票数 5

我试图在Asp上使用owin创建一个登录屏幕。这是控制器中的代码。

我甚至尝试过对这些值进行编码,但登录后输入仪表板时仍然可以得到401。

代码语言:javascript
复制
 [HttpPost]
    public ActionResult Login(string username, string password)
    {
        int userId = 0;
        string role = string.Empty;

        if (new UserManager().IsValid(username, password, ref userId, ref role))
        {
            var ident = new ClaimsIdentity(
              new[] { 
          // adding following 2 claim just for supporting default antiforgery provider
          new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
          new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),

          new Claim(ClaimTypes.Name,username),

          // optionally you could add roles if any
          new Claim(ClaimTypes.Role, role)

              },
              DefaultAuthenticationTypes.ApplicationCookie);

            HttpContext.GetOwinContext().Authentication.SignIn(
               new AuthenticationProperties { IsPersistent = false }, ident);
            return RedirectToAction("Dashboard"); // auth succeed 
        }
        // invalid username or password
        ModelState.AddModelError("", "invalid username or password");
        return View("Index", "_LayoutUnAuthorised");
    }

    [Authorize(Roles = "Default")]
    public ActionResult Dashboard()
    {



        return View();
    }

我的启动文件是空的,我是不是漏掉了什么?

代码语言:javascript
复制
public class Startup
{
    public void Configuration(IAppBuilder app)
    {

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-08 14:49:28

是的,您错过了Startup上的Owin Cookie配置:

代码语言:javascript
复制
public void Configuration(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Active,
        AuthenticationType = "ApplicationCookie",
        LoginPath = new PathString("/Account/LogOn"),
    });
}

安装Nuget软件包Microsoft.Owin.Security.Cookies然后你就可以走了

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37705290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档