OWIN简单登录无法登录

时间:2015-07-27 14:28:08

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

我正在尝试使用OWIN以简单的固定身份验证方式登录,但我似乎无法让它工作。我觉得我很容易错过一些东西,但似乎无法弄明白。除了保存此人的电子邮件地址并且不需要oauth之外,我不需要或关心任何事情,这是大多数教程和示例似乎显示的内容。当我运行以下代码时,我从未登录过,当我查看调试器时,身份验证声明始终为空。

我的剃刀网页:

@using System.Security.Claims;
@{

    Microsoft.Owin.IOwinContext ctx = Request.GetOwinContext();
    var usr = Request["text1"];
    if (IsPost && usr == "valid@user.com")
    {
        ctx.Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);

        var claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.Email, usr));
        claims.Add(new Claim(ClaimTypes.Name, var));
        var id = new ClaimsIdentity(claims, Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie);
        ctx.Authentication.SignIn(id);
        //FormsAuthentication.SetAuthCookie("valid@user.com", false); // Also fails
    }
    var IP = HttpContext.Current.User.Identity.IsAuthenticated.ToString();
    var text = "valid@user.com";

}
<!DOCTYPE html>
<html lang="en">
    <head><title></title></head>
    <body>
        <p>Are we Logged in? <strong>@IP</strong>.</p>
        <form action="" method="post">
            <p>
                <label for="text1">First Number:</label>
                <input type="text" name="text1" value="@text" />
            <p><input type="submit" value="Add" /></p>
        </form>
    </body>
</html>

启动类

using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Owin;

[assembly: OwinStartup(typeof(TypeScriptHTMLApp1.Startup1))]

namespace TypeScriptHTMLApp1
{
    public class Startup1
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType =DefaultAuthenticationTypes.ApplicationCookie, 
                AuthenticationMode = AuthenticationMode.Passive,
            });
        }
    }
}

为什么它总是返回false并且如何才能成功登录?

1 个答案:

答案 0 :(得分:1)

如果您在本地运行而不是在SSL下运行,请确保指示cookie处理程序不要求SSL。否则它永远不会发出cookie,看起来似乎没有任何事情发生。这让我上周难过。

   <system.identityModel.services>
      <federationConfiguration>
         <cookieHandler requireSsl="false" />
      </federationConfiguration>
   </system.identityModel.services>