ठीक है, धन्यवाद @Pinpoint:
अधिक जानकारी के लिए, आप भी इस ब्लॉग पोस्ट, Microsoft .NET वेब विकास और उपकरण ब्लॉग के लिए माइक Rousos द्वारा लिखित पढ़ सकते हैं।
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddOpenIddict<ApplicationUser, ApplicationRole, ApplicationDbContext>()
.DisableHttpsRequirement()
.EnableTokenEndpoint("/connect/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseOpenIddict();
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
Audience = "http://localhost:24624/",
Authority = "http://localhost:24624/"
});
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
ApplicationDbContext:
हालांकि यहां मेरे Startup.cs विन्यास है।सीएस:
public class ApplicationDbContext : OpenIddictDbContext<ApplicationUser, ApplicationRole>
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
ApplicationRole.cs:
public class ApplicationRole : IdentityRole
{
}
ApplicationUser.cs:
public class ApplicationUser : OpenIddictUser
{
}
ServiceController.cs:
[Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)]
[Route("api/service")]
public class ServiceController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
public ServiceController(UserManager<ApplicationUser> userManager)
{
_userManager = userManager;
}
[HttpGet]
[Route("getdata")]
public async Task<IActionResult> GetData()
{
var user = await _userManager.GetUserAsync(User);
if (user == null) return Ok("No user/not logged in");// if Authorize is not applied
return Ok(user);
}
}
यहाँ में महत्वपूर्ण ServiceController है। सीएस: [Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)]
@ पिनपॉइंट: मैंने ऐप का उपयोग नहीं किया .UUOAuthValidation() क्योंकि यह 302 लौटा रहा था और खाता/लॉगिन पर रीडायरेक्ट कर रहा था।
तो अब इसे इस तरह काम करता है:
- तक पहुँचने http://domain.com, उपयोगकर्ता रजिस्टर कर सकते हैं, लॉगिन, डेटा देखने, आदि
- उपयोगकर्ता मोबाइल एप्लिकेशन डाउनलोड कर सकते हैं, रजिस्टर, लॉगिन और डेटा प्राप्त
एपीआई पक्ष में उपयोगकर्ता पंजीकरण लॉगिन को कार्यान्वित करना आसान और सीधे आगे है।
समस्या यह थी कि फिडलर का उपयोग करना और http://domain.com/api/service/getdata पर जीईटी जारी करना 302 लौटा रहा था और खाता/लॉगिन पर रीडायरेक्ट कर रहा था। अगर मैं ऐप हटा देता हूं .UseIdentity(), तो अगर 401 अनधिकृत हो गया होगा लेकिन उपयोगकर्ता यूआई http://domain.com का उपयोग करके अब लॉगिन नहीं कर पाएगा। इस सेवा नियंत्रक को इस [Authorize(ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)]
को जोड़ने से समस्या हल हो गई।
@Pinpoint ऐप का लाभ क्या था .UUOAuthValidation()?
ऐप का उपयोग करके इस जवाब को देखें http://stackoverflow.com/questions/41551430/asp-net-core-no-redirect-on-api-auth-error/41551965#41551965, ऐप का उपयोग करके। मैपहेन एक और तरीका है ताकि आपको [प्राधिकरण (ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme)] –
मुझे आश्चर्य है कि "ActiveAuthenticationSchemes = OAuthValidationDefaults.AuthenticationScheme" को डिफ़ॉल्ट रूप से बनाना संभव है, ताकि मैं इसे प्रत्येक नियंत्रक पर सेट करने के लिए टोपी न करें। –