安装

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, "brockallen@gmail.com"));
//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();

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

  1. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

  2. ASP.NET没有魔法——ASP.NET Identity与授权

    一个完整的ASP.NET的请求中会存在身份验证(Authentication)阶段以及授权(Authorization)阶段,英文单词Authentication和Authorization非常相似, ...

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

    在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP. ...

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

    ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇   阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET ...

  5. 转载 ASP.NET MVC中使用ASP.NET Identity

    转载原地址: http://blog.jobbole.com/90695/ 在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identit ...

  6. Asp.net mvc 知多少(八)

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问[http: ...

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

    在这篇文章中,我将继续ASP.NET Identity 之旅,这也是ASP.NET Identity 三部曲的最后一篇.在本文中,将为大家介绍ASP.NET Identity 的高级功能,它支持声明式 ...

  8. ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇

    在前一篇文章中,我介绍了ASP.NET Identity 基本API的运用并创建了若干用户账号.那么在本篇文章中,我将继续ASP.NET Identity 之旅,向您展示如何运用ASP.NET Ide ...

  9. ASP.NET MVC WebApi接口授权验证

    对于很任何多开发者来说,不管是使用任何一种框架,或者是使用任何一种语言,都要使用面向接口编程.使用面向接口编程的时候,那么就会有很多的权限验证,用户验证等等. 特别是对于一些系统来说,别人想要对接你的 ...

随机推荐

  1. 公司内多个公众号实现账号互通(UnionID机制处理)

    场景: 由于用户在每个公众号上的OpenID都不一样,如果要实现判断判断某个用户在其中一个公众号上已经绑定过,那么就要借助(UnionID机制)的机制. 条件: 1.拥有微信开放平台账号,且认证(ht ...

  2. 微信公众平台教程和SDK收集

    教程收集: 1.Senparc(.net) http://www.cnblogs.com/szw/archive/2013/05/14/weixin-course-index.html 2.方倍工作室 ...

  3. Gulp.js----比Grunt更易用的前端构建工具

    Gulp.js----比Grunt更易用的前端构建工具 Grunt一直是前端构建工具,然而他也不是毫无缺陷的,gulp的作者 Eric Schoffstall 在他介绍 gulp.js 的 prese ...

  4. TrustedInstaller管理权限

    TrustedInstaller.exe实际上是“Windows Modules Installer”这个服务的进程,路径位于C:\Windows\servicing\TrustedInstaller ...

  5. nagios note

    server: nagios nagios_plugin nrpe ip:192.168.1.2 client nagios_plugin xinetd nrpe ip:192.168.1.3 一篇介 ...

  6. Timer

    timer类有三种 1.System.Windows.Forms.Timer  使用地方:Windows 窗体应用程序中,并且必须在窗口中使用.  2.System.Timers.Timer 使用地方 ...

  7. 数据结构算法C语言实现(二)---2.3线性表的链式表示和实现之单链表

    一.简述 [暂无] 二.头文件 #ifndef _2_3_part1_H_ #define _2_3_part1_H_ //2_3_part1.h /** author:zhaoyu email:zh ...

  8. FZU 1894 志愿者选拔(单调队列)

    传送门 Description 世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动.参加志愿者选拔的同学们排队接受面试官们的面试.参加面试的同学们按照先来先面试并且先结束的原则接受面试官们的考查. ...

  9. MATLAB cell & struct

    CELL cell的每个单元都可以存储任何数据,比如传递函数等.当然,存储矩阵更是没有问题的了.但是用cell数据类型之前,要先初始化. a=cell(n,m) 那么就把a初始化为一个n行m列的空ce ...

  10. Fix failed to start session in Ubuntu

    When you are at login, press Ctrl+Alt+F1. It will take you to command line interface from the GUI. I ...