asp.net mvc ClaimsIdentity 授权研究 (还是测试版 有bug)

安装

Microsoft.Owin.Host.SystemWeb

Identity.Core

Microsoft.Owin.Security.Cookies

在是startup.cs做如下修改

 public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }

        public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            //app.CreatePerOwinContext(ApplicationDbContext.Create);
           // app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            //app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Home/Index")//,
                ,
                CookieSecure = CookieSecureOption.Never
                //Provider = new CookieAuthenticationProvider
                //{
                //    // Enables the application to validate the security stamp when the user logs in.
                //    // This is a security feature which is used when you change a password or add an external login to your account.
                //    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                //        validateInterval: TimeSpan.FromMinutes(30),
                //        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                //}
            });
           //app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
            //app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // Enables the application to remember the second login verification factor such as phone or email.
            // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
            // This is similar to the RememberMe option when you log in.
            //app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

            // Uncomment the following lines to enable logging in with third party login providers
            //app.UseMicrosoftAccountAuthentication(
            //    clientId: "",
            //    clientSecret: "");

            //app.UseTwitterAuthentication(
            //   consumerKey: "",
            //   consumerSecret: "");

            //app.UseFacebookAuthentication(
            //   appId: "",
            //   appSecret: "");

            //app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
            //{
            //    ClientId = "",
            //    ClientSecret = ""
            //});
        }
    }

登录认证代码

ClaimsIdentity ci = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
ci.AddClaim(new Claim(ClaimTypes.Name, "Brock"));
ci.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
ci.AddClaim(new Claim(ClaimTypes.Email, "[email protected]"));
//var id = new ClaimsIdentity(ci, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
var ctx = HttpContext.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, ci);
return RedirectToAction("Test1");
//return View();
时间: 2024-12-30 14:22:22

asp.net mvc ClaimsIdentity 授权研究 (还是测试版 有bug)的相关文章

ASP.Net MVC 登录授权验证

public class Verify : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { var user = filterContext.HttpContext.Session["CurrentUser"]; if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymous

ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇(转)

ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇 阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET Identity ASP.NET Identity 其他API介绍 小节 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP.NET Identity的建

转载 ASP.NET MVC中使用ASP.NET Identity - 新西兰程序员 - 博客园

转载原地址: http://blog.jobbole.com/90695/ 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP.NET Identity的建立和使用,包括基础类的搭建和用户管理功能的实现-- http://myusermanagement.azurewebsites.net/Account/Login?ReturnUrl=%2F 点此

ASP.NET MVC 随想录—— 使用ASP.NET Identity实现基于声明的授权,高级篇

在这篇文章中,我将继续ASP.NET Identity 之旅,这也是ASP.NET Identity 三部曲的最后一篇.在本文中,将为大家介绍ASP.NET Identity 的高级功能,它支持声明式并且还可以灵活的与ASP.NET MVC 授权结合使用,同时,它还支持使用第三方来实现身份验证. 关于ASP.NET Identity 的基础知识,请参考如下文章: ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇 ASP.NET MVC 随想录——探索ASP.NE

七天学会ASP.NET MVC (四)——用户授权认证问题

小编应各位的要求,快马加鞭,马不停蹄的最终:七天学会 Asp.Net MVC 第四篇出炉.在第四天的学习中.我们主要了学习怎样在MVC中怎样实现认证授权等问题.本节主要讲了验证错误时的错误值,client验证,授权认证及登录注销功能的实现. 系列文章 七天学会ASP.NET MVC (一)--深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)--ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)--ASP.Net MVC 数据处理 七天学会ASP.NET

ASP.NET MVC下判断用户登录和授权的方法

日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在asp.net mvc中,微软虽然已经帮助开发者构建了ASP.NET Identity这样强大的验证授权框架,但是如果想定制更多的逻辑功能的话,还得自己动动手. 根据日常的开发经验,我总结了下面1种方法: 1 学习了很多人的代码后,发现在Controller里有一个OnActionExecuting方法,此

ASP.Net MVC 5 高级编程 第7章 成员资格、授权和安全性

第7章 成员资格.授权和安全性 7.1 安全性 ASP.NET MVC 提供了许多内置的保护机制(默认利用 HTML 辅助方法和Razor 语法进行 HTML编码以及请求验证等功能特性,以及通过基架构建的控制器白名单表单元素来防止重复提交攻击) 永远不要相信用户提交的任何数据. 实际的例子 每次渲染用户提交的数据的时候对其进行编码. 考虑好网站哪些部分允许用户匿名访问,哪些部分需要认证访问. 不要试图自己净化用户的HTML 输入,否则就会失败. 在不需要通过客户端脚本访问cookie时,使用HT

允许asp.net MVC报 错说明: 访问服务此请求所需的资源时出错。服务器可能未配置为访问所请求的 URL。错误消息 401.2。: 未经授权

运行mvc3程序报以下错误详细报错如下: "/"应用程序中的服务器错误. 访问被拒绝. 说明: 访问服务此请求所需的资源时出错.服务器可能未配置为访问所请求的 URL. 错误消息 401.2.: 未经授权: 服务器配置导致登录失败.请验证您是否有权基于您提供的凭据和 Web 服务器上启用的身份验证方法查看此目录或页.请与 Web 服务器的管理员联系以获得其他帮助. 版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.3

【ASP.NET MVC系列】浅谈ASP.NET MVC资源过滤和授权

最近比较忙,博客很久没更新了,很多博友问何时更新博文,因此,今天就花了点时间,写了本篇文章,但愿大家喜欢. 本篇文章不适合初学者,需要对ASP.NET MVC具有一定基础. 本篇文章主要从ASP.NET MVC 基架角度去分析MVC框架是如何实现资源过滤,资源授权,感兴趣的,欢迎阅读. 相关文章,请参与ASP.NET MVC系列 一 ASP.NET MVC框架验证机制 为了更加透彻地了解MVC的过滤机制,我简要地画了如下UML图. 下面,我们根据如上的UML图来简要分析一下. (一)MVC基架过