我正在尝试使用OWIN通过无效的简单固定身份验证登录,但是我似乎根本无法使它正常工作。我觉得我很容易遗漏一些东西,但似乎无法弄清楚。除了保存该人的电子邮件地址,并且不需要oauth之外,我不需要或关心任何事情,而这正是大多数教程和示例所显示的。当我运行以下代码时,我从未登录,而在调试器中查看时,身份验证声明始终为空。
我的Razor网页:
@using System.Security.Claims;
@{
Microsoft.Owin.IOwinContext ctx = Request.GetOwinContext();
var usr = Request["text1"];
if (IsPost && usr == "[email protected]")
{
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("[email protected]", false); // Also fails
}
var IP = HttpContext.Current.User.Identity.IsAuthenticated.ToString();
var text = "[email protected]";
}
<!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,如何使它成功登录?
如果您在本地运行并且不在SSL下运行,请确保指示Cookie处理程序不要使用SSL。否则,它将永远不会发出Cookie,并且似乎什么也没有发生。就在上周,我为此感到难过。
<system.identityModel.services>
<federationConfiguration>
<cookieHandler requireSsl="false" />
</federationConfiguration>
</system.identityModel.services>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句