C# 使用身份验证ASP.NET MVC中的用户
我对实际的身份验证工作方式非常困惑,以至于C# 使用身份验证ASP.NET MVC中的用户,c#,authentication,asp.net-mvc-5,asp.net-identity,C#,Authentication,Asp.net Mvc 5,Asp.net Identity,我对实际的身份验证工作方式非常困惑,以至于[Authorize]无法将我重定向到登录页面 以下是我的配置: public class IdentityConfig { public void Configuration(IAppBuilder app) { app.CreatePerOwinContext(() => new MyANTon.DataContext.AntContext()); app.CreatePerOwinContext
[Authorize]
无法将我重定向到登录页面
以下是我的配置:
public class IdentityConfig
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() => new MyANTon.DataContext.AntContext());
app.CreatePerOwinContext<UserManager>(UserManager.Create);
app.CreatePerOwinContext<RoleManager<AppRole>>((options, context) =>
new RoleManager<AppRole>(
new RoleStore<AppRole>(context.Get<MyANTon.DataContext.AntContext>())));
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Home/Login"),
});
}
}
当你想
登录到你的网站
时,你可以将你的令牌
发送到客户机
,你可以使用它来请求和响应许多请求,换句话说,你必须使用服务器端登录
但是,当您想从网站注销时,您的客户端必须知道您想注销这不仅仅适用于服务器,客户端应该在注销时执行此操作
我想向您推荐代币JWT
如果您想了解JWT,请单击
如果您愿意,我将为您创建一个示例。您正朝着正确的方向前进,您正在做的是使用OAuth简化标记的映射,并让OWin处理浏览器信息。那么,通过使用[Authorize]属性,就像您正在做的那样,您是如何处理身份的签名的?与上面提到的表单身份验证一样,您仍然需要创建标识/声明令牌。在我的项目中,我做了这样的事情
protected void IdentitySignin(IUserModel userModel, string providerKey = null, bool isPersistent = true)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, userModel.Id.ToString()),
new Claim(ClaimTypes.Name, userModel.UserName),
new Claim("UserContext", userModel.ToString())
};
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = isPersistent,
ExpiresUtc = DateTime.UtcNow.AddDays(7)
}, identity);
}
My AuthenticationManager的定义如下:
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
它是Microsoft.OWin.IOwinContext的一部分,如果引用不存在,则必须添加该引用
您可以通过web.config文件或基本控制器处理未经授权的用户,我选择了基本控制器选项,如下所示:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (UserContext == null || UserContext.Id.Equals(Guid.Empty))
{
if (filterContext.Controller.GetType() == typeof(AccountController) || filterContext.Controller.GetType() == typeof(HomeController)) return;
filterContext.Result = new RedirectResult("/Home/Index");
return;
}
}
但是,如果需要,也可以通过authorized属性来完成。这个链接将详细介绍如何处理Oauth和Asp.NETMVC,一开始可能会让人望而生畏,但如果您决定将其他提供程序合并到发布版本中,它将为您提供一个很好的使用其他提供程序的布局
谢谢,我接受了你的回答。为了让它发挥作用,我不得不改变一些事情。我使用了大多数方法static
(严格来说不是必需的),而不是编写HttpContext.GetOwinContext().Authentication代码>我必须编写HttpContext.Current.GetOwinContext().Authentication代码>。除此之外,从我现在看到的情况来看,它工作得很好。快速跟进问题:我真的不能在razor页面上使用基于角色的授权,比如[Authorize(roles=“admin”)]
?我会在这里查看这两个网站,然后。授权属性是令人难以置信的模块化,可以方便地执行几乎任何任务。我甚至还有一个,它将用户的IP地址与一个全局数据库进行比较,以根据他们的国家/地区重新定向。@KevinBBurns会话保持2分钟怎么样?我们可以用这种方法设置会话到期时间吗?@hexadecimal我想你想要的是滑动到期,你能看看我的问题吗?如果它适合我的情况,我愿意使用它。谢谢…嗨,你们知道授权属性吗?是的,我用的是ASP.NET身份。你们用的是JWT令牌吗??或者使用不同的?我没有使用JWT令牌,但如果它更合适,我会使用它(必须适用于MVC而不是Core)。
protected void IdentitySignout()
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie,
DefaultAuthenticationTypes.ExternalCookie);
}
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (UserContext == null || UserContext.Id.Equals(Guid.Empty))
{
if (filterContext.Controller.GetType() == typeof(AccountController) || filterContext.Controller.GetType() == typeof(HomeController)) return;
filterContext.Result = new RedirectResult("/Home/Index");
return;
}
}