用到.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) });
这样还是不行,请问还需要配置哪里吗,或是我这代码有问题吗?
web.config 取人formauth的那个handler没有remove,并且add了。
还是没弄出来,能详细些吗?
@痛快:
看看web.config,有没有,有的话就删掉
<system.webServer>
<remove name="FormsAuthentication" />
@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>
@痛快:
登录完成后,在浏览器看一下相应的response,里面是否有带上set-cookie
登录完成之后,随便访问一个页面,浏览器看一下request是否有带上cookie
@calvinK: 没有任何值