首页 新闻 赞助 找找看

.net core 身份认证问题

0
悬赏园豆:10 [已解决问题] 解决于 2016-07-18 11:24

用到.net core的身份认证,但是Cookies为空,怎么解决?

代码如下:

登录代码:

var userId = "username";
            var identity = new ClaimsIdentity("password");
            identity.AddClaim(new Claim(ClaimTypes.Name, userId));
            var cp = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userId) }, CookieAuthenticationDefaults.AuthenticationScheme));
            HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, cp);

Startup中增加:

 app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
                LoginPath = new PathString("/Account/Unauthorized/"),
                AccessDeniedPath = new PathString("/Account/Forbidden/"),
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                CookieHttpOnly = true,
                CookieName = "MyCookie",
                ExpireTimeSpan = TimeSpan.FromHours(2)
            });

这样还是不行,请问还需要配置哪里吗,或是我这代码有问题吗?

痛快的主页 痛快 | 初学一级 | 园豆:71
提问于:2016-07-14 22:28
< >
分享
最佳答案
0

web.config 取人formauth的那个handler没有remove,并且add了。

收获园豆:10
czd890 | 专家六级 |园豆:14292 | 2016-07-14 22:56

还是没弄出来,能详细些吗?

痛快 | 园豆:71 (初学一级) | 2016-07-15 14:15

@痛快: 

看看web.config,有没有,有的话就删掉

<system.webServer>

<remove name="FormsAuthentication" />

czd890 | 园豆:14292 (专家六级) | 2016-07-15 15:01

@calvinK: 没有这个没有加

Web.config代码:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.web>
<!--用户登录-->
    <authentication mode="Forms">
      <forms name="TC" loginUrl="~/Account/Login" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" timeout="60" />
    </authentication>
    <!--<authorization>
      <deny users="?" />
    </authorization>-->
   <!-- <sessionState mode="SQLServer" sqlConnectionString="server=127.0.0.1;database=SessionM;uid=sa;pwd=abc@123;" timeout="160" cookieless="false" allowCustomSqlDatabase="true" />-->
    <machineKey validation="SHA1" validationKey="6688D62FF3D6108F45F4288B2BD2A09D894F49E8" decryptionKey="CD25D72BDF483FD0777A1968B718BA369729788408A342C9" />
    <!--用户登录-->
  </system.web>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>
痛快 | 园豆:71 (初学一级) | 2016-07-15 17:04

@痛快: 

登录完成后,在浏览器看一下相应的response,里面是否有带上set-cookie

登录完成之后,随便访问一个页面,浏览器看一下request是否有带上cookie

czd890 | 园豆:14292 (专家六级) | 2016-07-15 17:38

@calvinK: 没有任何值

痛快 | 园豆:71 (初学一级) | 2016-07-16 21:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册