加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-identity – 由于其保护级别,Microsoft.Owin.Security.A

发布时间:2020-12-16 09:25:51 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试实现ASP.NET Identity 2.0.我在VS 2012中创建了一个MVC4项目,并使用Nu Get命令安装了ASP.NET Identity 2.0软件包.我收到编译错误消息“Microsoft.Owin.Security.AuthenticationManager”由于其保护级别而无法访问,具有以下代码. 获取编译错误,因为
我正在尝试实现ASP.NET Identity 2.0.我在VS 2012中创建了一个MVC4项目,并使用Nu Get命令安装了ASP.NET Identity 2.0软件包.我收到编译错误消息“Microsoft.Owin.Security.AuthenticationManager”由于其保护级别而无法访问,具有以下代码.

获取编译错误,因为AuthenticationManager是程序集EntityFramework.dll中的内部类.
因此,我可以从AuthenticationManager派生一个类并调用公共方法SignIn()和SignOut().

在同一个程序集中有一个AuthenticationManagerExtensions类.我怎样才能将它用于SignIn和SignOut?

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Threading.Tasks;
using Microsoft.Owin.Security;

    private async Task SignInAsync(ApplicationUser user,bool isPersistent)
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

        var identity = await UserManager.CreateIdentityAsync(
           user,DefaultAuthenticationTypes.ApplicationCookie);

        AuthenticationManager.SignIn(
           new AuthenticationProperties()
           {
               IsPersistent = isPersistent
           },identity);
    }

解决方法

此处使用的AuthenticationManager是当前Owin上下文的Authentication对象.通过在同一个类中添加以下属性来解决此问题.

private IAuthenticationManager AuthenticationManager
    {
        get
        {
            return HttpContext.GetOwinContext().Authentication;
        }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读